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 maxN 500005
using namespace std;
int seg[4*maxN],a[2*maxN],b[maxN],n,i,d,ans,sum;
void update(int n,int l,int d,int x,int y){
if(l==d) seg[n]=y;
else{
int m=(l+d)/2;
if(x<=m) update(2*n,l,m,x,y);
else update(2*n+1,m+1,d,x,y);
seg[n]=max(seg[2*n],seg[2*n+1]);
}
}
int query(int n,int l,int d,int x,int y){
if(d<x || l>y || x>y) return 0;
if(l>=x && y>=d) return seg[n];
int m=(l+d)/2;
return max(query(2*n,l,m,x,y),query(2*n+1,m+1,d,x,y));
}
int main()
{
std::ios_base::sync_with_stdio(false);
cin>>n;
d=n/2;
for(i=0;i<n;i++) {cin>>a[i]; a[n+i]=a[i]; sum+=a[i];}
for(i=0;i<d;i++) b[0]+=a[i];
update(1,0,n-1,0,b[0]);
for(i=1;i<n;i++) {b[i]=b[i-1]-a[i-1]+a[i+d-1]; update(1,0,n-1,i,b[i]);}
for(i=0;i<n;i++){
ans=max(ans,sum-max(query(1,0,n-1,0,i-d),query(1,0,n-1,i+1,min(n-1,n+i-d))));
}
cout<<ans<<endl;
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... |