#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<long long> V(n+1, LLONG_MAX);
long long s=0;
int res=-1;
for(int i=1; i<=n; i++) {
int a;
cin >> a;
s+=a;
// cout << "s=" << s << endl;
if (s<0) continue;
auto it = upper_bound(V.begin(), V.end(), s);
*it=s;
res=it-V.begin();
// cout << i - (lower_bound(V.begin(), V.end(), LLONG_MAX)-V.begin()) << endl;
}
// cout << n - (lower_bound(V.begin(), V.end(), LLONG_MAX)-V.begin()) << endl;
if (s<0) cout << -1 << endl;
else cout << n - res -1 << endl;
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 | #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<long long> V(n+1, LLONG_MAX); long long s=0; int res=-1; for(int i=1; i<=n; i++) { int a; cin >> a; s+=a; // cout << "s=" << s << endl; if (s<0) continue; auto it = upper_bound(V.begin(), V.end(), s); *it=s; res=it-V.begin(); // cout << i - (lower_bound(V.begin(), V.end(), LLONG_MAX)-V.begin()) << endl; } // cout << n - (lower_bound(V.begin(), V.end(), LLONG_MAX)-V.begin()) << endl; if (s<0) cout << -1 << endl; else cout << n - res -1 << endl; return 0; } |
English