#include <ctime> #include <cassert> #include <iostream> #include <iomanip> #include <vector> #include <list> #include <algorithm> #include <map> using namespace std; const int MAXN = 500000; const long long MAXA = 9223372036854775807; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long V[MAXN]; long long S[MAXN+2]; bool reversed(const long long &a, const long long &b) { return a > b; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; FOR(i, n) { cin >> V[i]; S[i] = -MAXA; } S[0] = 0; S[n] = S[n+1] = -MAXA; long long base = 0; FOR (i, n) { auto it = upper_bound(S, S + n + 1, -base, reversed); // cout << V[i] << " at " << it - S << " / " << base << endl; if (base >= 0) { *it = -base; // tak naprawdę = V[i] } base += V[i]; // FOR(j, n+2) if (S[j] > -1000000000) cout << S[j] + base << " "; // cout << " (" << base << ")" <<endl; } if (base < 0) cout << -1 << endl; else { auto it = upper_bound(S, S + n+1, -base, reversed); cout << n + 1 - (it - S) << endl; } }
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 | #include <ctime> #include <cassert> #include <iostream> #include <iomanip> #include <vector> #include <list> #include <algorithm> #include <map> using namespace std; const int MAXN = 500000; const long long MAXA = 9223372036854775807; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long V[MAXN]; long long S[MAXN+2]; bool reversed(const long long &a, const long long &b) { return a > b; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; FOR(i, n) { cin >> V[i]; S[i] = -MAXA; } S[0] = 0; S[n] = S[n+1] = -MAXA; long long base = 0; FOR (i, n) { auto it = upper_bound(S, S + n + 1, -base, reversed); // cout << V[i] << " at " << it - S << " / " << base << endl; if (base >= 0) { *it = -base; // tak naprawdę = V[i] } base += V[i]; // FOR(j, n+2) if (S[j] > -1000000000) cout << S[j] + base << " "; // cout << " (" << base << ")" <<endl; } if (base < 0) cout << -1 << endl; else { auto it = upper_bound(S, S + n+1, -base, reversed); cout << n + 1 - (it - S) << endl; } } |