#include <bits/stdc++.h>
using namespace std;
const int N = 500*1000+5;
int a[N];
long long pref[N];
long long ANS[N];
long long prefmx[N];
long long sufmx[N];
long long t[4*N];
void update(int v,int s,int e,int l,int r,long long val){
if(l>r) return;
if(s == l && e == r){
t[v] = val;
return;
}
int m = (s+e)/2;
update(v*2,s,m,l,min(m,r),val);
update(v*2+1,m+1,e,max(l,m+1),r,val);
t[v] = max(t[v*2],t[v*2+1]);
}
long long get(int v,int s,int e,int l,int r){
if(l>r) return 0;
if(s == l && e == r) return t[v];
int m = (s+e)/2;
return max(get(v*2,s,m,l,min(m,r)),
get(v*2+1,m+1,e,max(l,m+1),r));
}
int main(){
ios_base::sync_with_stdio(false);
int n;
cin>>n;
long long all = 0;
for(int i = 1;i<=n;i++) {
cin>>a[i];
all+=a[i];
pref[i] = pref[i-1]+a[i];
}
for(int j = 1;j<=n;j++){
int jj = j;
int qn = 0;
long long cur = 0;
int l = j;
int r = j+n/2-1;
if(r>n) r = (r-n);
if(r>=l){
// if(i<l || i>r){
cur = pref[r]-pref[l-1];
// }
}
else{
// if(i>r && i<l){
cur+=(pref[r]);
cur+=(pref[n]-pref[l-1]);
// }
}
// mx = max(mx,cur);
ANS[j] = cur;
update(1,1,n,j,j,cur);
}
for(int i = 1;i<=n;i++){
prefmx[i] = max(prefmx[i-1],ANS[i]);
}
for(int i = n;i>=1;i--){
sufmx[i] = max(sufmx[i+1],ANS[i]);
}
long long ans = 0;
for(int i = 1;i<=n;i++){
int pos = i-n/2;
long long cur = 0;
if(pos>=1){
cur = max(cur,prefmx[i]);
cur = max(cur,sufmx[i+1]);
}
else{
pos = n+pos;
long long x = get(1,1,n,i+1,pos);
cur = max(cur,x);
}
ans = max(ans,all-cur);
}
cout<<ans<<endl;
return 0;
}
Compilation message
hac.cpp: In function 'int main()':
hac.cpp:40:17: warning: unused variable 'jj' [-Wunused-variable]
int jj = j;
^~
hac.cpp:41:17: warning: unused variable 'qn' [-Wunused-variable]
int qn = 0;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
41 ms |
5240 KB |
Output is correct |
5 |
Correct |
102 ms |
12280 KB |
Output is correct |
6 |
Correct |
130 ms |
14456 KB |
Output is correct |
7 |
Correct |
169 ms |
20496 KB |
Output is correct |
8 |
Correct |
275 ms |
28412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |