#pragma GCC optimize("O3") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define FOR(a, b, c) for(int a = b; a < c; ++a) #define PB push_back #define MP make_pair #define INF (int)1e9+7 #define LLINF 2e18+7 #define ALL(a) a.begin(), a.end() #define SIZE(a) (int)a.size() typedef unsigned long long ULL; typedef long long LL; typedef long double LD; using namespace std; //#define DEBUG #define cerr if(0) cerr const int H = 1 << 19; LL DP[H << 1]; void addRange(int l, int r, LL val) { l += H; r += H; while(l < r) { if(l & 1) { DP[l] += val; l++; } if(!(r & 1)) { DP[r] += val; r--; } l >>= 1; r >>= 1; } if(l == r) DP[l] += val; } LL getVal(int v) { v += H; LL res = 0; while(v) { res += DP[v]; v >>= 1; } return res; } int main() { #ifndef DEBUG BOOST; #endif LL n; cin >> n; int b = n; FOR(i, 1, n + 1) { LL a; cin >> a; int minn = b, maxx = n; int oldb = b; while(minn < maxx) { int z = (minn + maxx) / 2; LL vz = getVal(z); if(vz + a < 0) minn = z + 1; else maxx = z; } if(minn <= n and getVal(minn) + a >= 0) { if(minn == b and a >= 0) { b--; } } else { minn = -1; } cerr << "minimum is: " << minn << "\n"; addRange(oldb, n, a); if(minn != -1) { LL val = getVal(minn - 1); if(val < 0) { addRange(minn - 1, minn - 1, -val); } } cerr << "DEBUG:\n"; FOR(j, b, n + 1) { cerr << getVal(j) << "\n"; } cerr << "\n"; } FOR(i, b, n + 1) { if(getVal(i) >= 0) { cout << i << "\n"; return 0; } } cout << -1 << "\n"; return 0; }
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 | #pragma GCC optimize("O3") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define FOR(a, b, c) for(int a = b; a < c; ++a) #define PB push_back #define MP make_pair #define INF (int)1e9+7 #define LLINF 2e18+7 #define ALL(a) a.begin(), a.end() #define SIZE(a) (int)a.size() typedef unsigned long long ULL; typedef long long LL; typedef long double LD; using namespace std; //#define DEBUG #define cerr if(0) cerr const int H = 1 << 19; LL DP[H << 1]; void addRange(int l, int r, LL val) { l += H; r += H; while(l < r) { if(l & 1) { DP[l] += val; l++; } if(!(r & 1)) { DP[r] += val; r--; } l >>= 1; r >>= 1; } if(l == r) DP[l] += val; } LL getVal(int v) { v += H; LL res = 0; while(v) { res += DP[v]; v >>= 1; } return res; } int main() { #ifndef DEBUG BOOST; #endif LL n; cin >> n; int b = n; FOR(i, 1, n + 1) { LL a; cin >> a; int minn = b, maxx = n; int oldb = b; while(minn < maxx) { int z = (minn + maxx) / 2; LL vz = getVal(z); if(vz + a < 0) minn = z + 1; else maxx = z; } if(minn <= n and getVal(minn) + a >= 0) { if(minn == b and a >= 0) { b--; } } else { minn = -1; } cerr << "minimum is: " << minn << "\n"; addRange(oldb, n, a); if(minn != -1) { LL val = getVal(minn - 1); if(val < 0) { addRange(minn - 1, minn - 1, -val); } } cerr << "DEBUG:\n"; FOR(j, b, n + 1) { cerr << getVal(j) << "\n"; } cerr << "\n"; } FOR(i, b, n + 1) { if(getVal(i) >= 0) { cout << i << "\n"; return 0; } } cout << -1 << "\n"; return 0; } |