This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int N = 5e5 + 5;
const long long M = (long long)1e15;
int n,a[N];
long long pref[N];
pair<int,long long> dp[N];
unordered_map< long long,pair<long long,long long> > t;
void upd(long long pos,pair<long long,long long> val,long long v = 1,long long tl = 1,long long tr = M){
if(tl == tr){
t[v] = val;
return;
}
long long tm = (tl + tr) / 2;
if(pos <= tm) upd(pos,val,v + v,tl,tm);
else upd(pos,val,v + v + 1,tm + 1,tr);
t[v] = max(t[v + v],t[v + v + 1]);
}
pair<int,long long> get(long long l,long long r,long long v = 1,long long tl = 1,long long tr = M){
if(l <= tl && tr <= r) return t[v];
if(tl > r || tr < l) return make_pair(0,0);
long long tm = (tl + tr) / 2;
return max(get(l,r,v + v,tl,tm),get(l,r,v + v + 1,tm + 1,tr));
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
dp[1] = make_pair(1,a[1]);
upd(pref[1] + dp[1].S,make_pair(dp[1].F,1));
for(int i = 2; i <= n; i++){
dp[i] = make_pair(dp[i - 1].F,dp[i - 1].S + a[i]);
pair<int,long long> cur = get(1,pref[i]);
if(cur.F > 0) dp[i] = make_pair(cur.F + 1,pref[i] - pref[cur.S]);
upd(pref[i] + dp[i].S,make_pair(dp[i].F,i));
}
cout << dp[n].F;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |