#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[pos]);
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 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
4 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
4 ms |
760 KB |
Output is correct |
26 |
Correct |
4 ms |
632 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
4 ms |
760 KB |
Output is correct |
31 |
Correct |
4 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
760 KB |
Output is correct |
4 |
Correct |
41 ms |
5328 KB |
Output is correct |
5 |
Correct |
102 ms |
11512 KB |
Output is correct |
6 |
Correct |
130 ms |
13252 KB |
Output is correct |
7 |
Correct |
161 ms |
19192 KB |
Output is correct |
8 |
Correct |
271 ms |
26228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
4 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
4 ms |
760 KB |
Output is correct |
26 |
Correct |
4 ms |
632 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
4 ms |
760 KB |
Output is correct |
31 |
Correct |
4 ms |
760 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
4 ms |
760 KB |
Output is correct |
35 |
Correct |
41 ms |
5328 KB |
Output is correct |
36 |
Correct |
102 ms |
11512 KB |
Output is correct |
37 |
Correct |
130 ms |
13252 KB |
Output is correct |
38 |
Correct |
161 ms |
19192 KB |
Output is correct |
39 |
Correct |
271 ms |
26228 KB |
Output is correct |
40 |
Correct |
6 ms |
1016 KB |
Output is correct |
41 |
Correct |
10 ms |
1656 KB |
Output is correct |
42 |
Correct |
15 ms |
2168 KB |
Output is correct |
43 |
Correct |
104 ms |
12472 KB |
Output is correct |
44 |
Correct |
272 ms |
28408 KB |
Output is correct |
45 |
Correct |
50 ms |
6264 KB |
Output is correct |
46 |
Correct |
162 ms |
20548 KB |
Output is correct |
47 |
Correct |
270 ms |
28408 KB |
Output is correct |
48 |
Correct |
267 ms |
28536 KB |
Output is correct |
49 |
Correct |
272 ms |
27768 KB |
Output is correct |
50 |
Correct |
262 ms |
27768 KB |
Output is correct |