#include <bits/stdc++.h> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using PII = pair<int,int>; using PLL = pair<LL,LL>; using VI = vector<int>; using VLL = vector<LL>; template<class T> using V = vector<T>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } const int inf = 1e9; const LL infl = 1e18; const int N = int(5e5) + 1; mt19937 rnd; int p[N], l[N], r[N]; int mn[N], val[N], f; LL key[N]; void update(int i) { mn[i] = val[i]; amin(mn[i], mn[l[i]]); amin(mn[i], mn[r[i]]); } int join(int i, int j) { if (!i) return j; if (!j) return i; if (p[i] < p[j]) { r[i] = join(r[i], j); update(i); return i; } l[j] = join(i, l[j]); update(j); return j; } // [< v, >= v] PII split(int k, LL x) { if (!k) return MP(0, 0); PII s; if (key[k] < x) { s = split(exchange(r[k], 0), x); update(k); return MP(join(k, s.FS), s.ND); } s = split(exchange(l[k], 0), x); update(k); return MP(s.FS, join(s.ND, k)); } int find(int k, LL x) { if (!k || key[k] == x) return k; return find(x < key[k] ? l[k] : r[k], x); } int singleton(LL x, int y) { f++; key[f] = x; val[f] = y; update(f); return f; } int n, t, ans = inf, a[N], b[N], dp[N]; LL s[N]; int get_min(LL x) { int res = inf; auto [t1, t2] = split(t, x + 1); if (t1) res = mn[t1]; t = join(t1, t2); return res; } int main() { scanf("%d", &n); uint32_t h = 2137; FOR(i, 1, n) { scanf("%d", &a[i]); h = (31u * h + a[i]); s[i] += s[i - 1] + a[i]; } rnd.seed(h); iota(p, p + N, 0); shuffle(p, p + N, rnd); mn[0] = inf; val[0] = inf; if (s[n] < 0) { puts("-1"); exit(0); } int k = inf; FORD(i, 1, n) { b[i] = k; if (a[i]) k = i; } if (k == inf) { puts("0"); exit(0); } t = singleton(0, -k); FOR(i, 1, n) if (a[i]) { dp[i] = get_min(s[i]); if (dp[i] == inf) continue; dp[i] += i; if (b[i] != inf) { int y = min(val[find(t, s[i])], dp[i] - b[i]); auto [t1, t2] = split(t, s[i]); t = join(t1, join(singleton(s[i], y), split(t2, s[i] + 1).ND)); } } FORD(i, 1, n) if (a[i]) { amin(ans, dp[i]); if (a[i] < 0) break; } printf("%d\n", ans); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <bits/stdc++.h> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using PII = pair<int,int>; using PLL = pair<LL,LL>; using VI = vector<int>; using VLL = vector<LL>; template<class T> using V = vector<T>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } const int inf = 1e9; const LL infl = 1e18; const int N = int(5e5) + 1; mt19937 rnd; int p[N], l[N], r[N]; int mn[N], val[N], f; LL key[N]; void update(int i) { mn[i] = val[i]; amin(mn[i], mn[l[i]]); amin(mn[i], mn[r[i]]); } int join(int i, int j) { if (!i) return j; if (!j) return i; if (p[i] < p[j]) { r[i] = join(r[i], j); update(i); return i; } l[j] = join(i, l[j]); update(j); return j; } // [< v, >= v] PII split(int k, LL x) { if (!k) return MP(0, 0); PII s; if (key[k] < x) { s = split(exchange(r[k], 0), x); update(k); return MP(join(k, s.FS), s.ND); } s = split(exchange(l[k], 0), x); update(k); return MP(s.FS, join(s.ND, k)); } int find(int k, LL x) { if (!k || key[k] == x) return k; return find(x < key[k] ? l[k] : r[k], x); } int singleton(LL x, int y) { f++; key[f] = x; val[f] = y; update(f); return f; } int n, t, ans = inf, a[N], b[N], dp[N]; LL s[N]; int get_min(LL x) { int res = inf; auto [t1, t2] = split(t, x + 1); if (t1) res = mn[t1]; t = join(t1, t2); return res; } int main() { scanf("%d", &n); uint32_t h = 2137; FOR(i, 1, n) { scanf("%d", &a[i]); h = (31u * h + a[i]); s[i] += s[i - 1] + a[i]; } rnd.seed(h); iota(p, p + N, 0); shuffle(p, p + N, rnd); mn[0] = inf; val[0] = inf; if (s[n] < 0) { puts("-1"); exit(0); } int k = inf; FORD(i, 1, n) { b[i] = k; if (a[i]) k = i; } if (k == inf) { puts("0"); exit(0); } t = singleton(0, -k); FOR(i, 1, n) if (a[i]) { dp[i] = get_min(s[i]); if (dp[i] == inf) continue; dp[i] += i; if (b[i] != inf) { int y = min(val[find(t, s[i])], dp[i] - b[i]); auto [t1, t2] = split(t, s[i]); t = join(t1, join(singleton(s[i], y), split(t2, s[i] + 1).ND)); } } FORD(i, 1, n) if (a[i]) { amin(ans, dp[i]); if (a[i] < 0) break; } printf("%d\n", ans); } |