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 = (int)5e5 + 5;
const long long M = (long long)6e14;
int n,timer = 1,a[N],L[N * 20],R[N * 20];
pair<int,int> t[N * 20];
long long pref[N];
pair<int,long long> dp[N];
pair<int,int> ZeroPair = {0,0};
void upd(int v,long long tl,long long tr,long long pos,pair<int,int> val){
if(tl == tr){
t[v] = val;
return;
}
long long tm = (tl + tr) >> 1;
if(pos <= tm){
if(!L[v]) L[v] = ++timer;
upd(L[v],tl,tm,pos,val);
}
else{
if(!R[v]) R[v] = ++timer;
upd(R[v],tm + 1,tr,pos,val);
}
t[v] = max(t[L[v]],t[R[v]]);
}
pair<int,int> get(int v,long long tl,long long tr,long long l,long long r){
if(l <= tl && tr <= r) return t[v];
if(tl > r || tr < l) return ZeroPair;
long long tm = (tl + tr) >> 1;
return max(get(L[v],tl,tm,l,r),get(R[v],tm + 1,tr,l,r));
}
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] = {1,a[1]};
upd(1,1,M,pref[1] + dp[1].S,make_pair(dp[1].F,1));
for(int i = 2; i <= n; i++){
dp[i] = {dp[i - 1].F,dp[i - 1].S + a[i]};
pair<int,int> cur = get(1,1,M,1,pref[i]);
if(cur.F > 0) dp[i] = {cur.F + 1,pref[i] - pref[cur.S]};
upd(1,1,M,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... |