#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
int pre[500005],suf[500005],arr[500005];
int MX[22][500005],MX2[22][500005];
int n,mx,ans,j;
void sparsMX(){
for(int k=1;k<=20;k++){
for(int i=0;i+(1ll<<k-1)<=n;i++) MX[k][i]=max(MX[k-1][i],MX[k-1][i+(1ll<<k-1)]);
for(int i=0;i+(1ll<<k-1)<=n+1;i++) MX2[k][i]=max(MX2[k-1][i],MX2[k-1][i+(1ll<<k-1)]);
}
}
int findMX(int l,int r){
int siz=log2(((r-l)+1));
return max(MX[siz][l],MX[siz][r+1-(1ll<<siz)]);
}
int findMX2(int l,int r){
int siz=log2(((r-l)+1));
return max(MX2[siz][l],MX2[siz][r+1-(1ll<<siz)]);
}
int calc1(int i){
if(n-j<=i) return (i-(n-j));
else return 0;
}
int calc2(int i){
if(i<=j) return (n+1)-((j-i)+1);
else return n+1;
}
signed main()
{
cin>>n;
j=n/2;
for(int i=1;i<=n;i++){
cin>>arr[i];
pre[i]=pre[i-1]+arr[i];
}
for(int i=n;i>=1;i--) suf[i]=suf[i+1]+arr[i];
for(int i=0;i<=n;i++){
if(i>=j) MX[0][i]=pre[i]-pre[i-j];
else{
MX[0][i]=pre[i]+suf[n+1-(j-i)];
}
}
for(int i=n+1;i>=1;i--){
if((n-i)+1>=j) MX2[0][i]=suf[i]-suf[i+j];
else{
MX2[0][i]=suf[i]+pre[j-((n-i)+1)];
}
}
sparsMX();
for(int i=1;i<=n;i++){
ans=max(ans,pre[n]-max(findMX(calc1(i),i-1),findMX2(i+1,calc2(i))));
}
cout<<ans<<endl;
return 0;
}
Compilation message
hac.cpp: In function 'void sparsMX()':
hac.cpp:10:30: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
10 | for(int i=0;i+(1ll<<k-1)<=n;i++) MX[k][i]=max(MX[k-1][i],MX[k-1][i+(1ll<<k-1)]);
| ~^~
hac.cpp:10:83: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
10 | for(int i=0;i+(1ll<<k-1)<=n;i++) MX[k][i]=max(MX[k-1][i],MX[k-1][i+(1ll<<k-1)]);
| ~^~
hac.cpp:11:30: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
11 | for(int i=0;i+(1ll<<k-1)<=n+1;i++) MX2[k][i]=max(MX2[k-1][i],MX2[k-1][i+(1ll<<k-1)]);
| ~^~
hac.cpp:11:88: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
11 | for(int i=0;i+(1ll<<k-1)<=n+1;i++) MX2[k][i]=max(MX2[k-1][i],MX2[k-1][i+(1ll<<k-1)]);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2472 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2472 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
2 ms |
5724 KB |
Output is correct |
24 |
Correct |
1 ms |
3164 KB |
Output is correct |
25 |
Correct |
2 ms |
3676 KB |
Output is correct |
26 |
Correct |
2 ms |
3676 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
2 ms |
5724 KB |
Output is correct |
31 |
Correct |
2 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
24 ms |
24920 KB |
Output is correct |
5 |
Correct |
59 ms |
63316 KB |
Output is correct |
6 |
Correct |
75 ms |
79956 KB |
Output is correct |
7 |
Correct |
96 ms |
96512 KB |
Output is correct |
8 |
Correct |
163 ms |
164432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2472 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
2 ms |
5724 KB |
Output is correct |
24 |
Correct |
1 ms |
3164 KB |
Output is correct |
25 |
Correct |
2 ms |
3676 KB |
Output is correct |
26 |
Correct |
2 ms |
3676 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
2 ms |
5724 KB |
Output is correct |
31 |
Correct |
2 ms |
3676 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
5724 KB |
Output is correct |
35 |
Correct |
24 ms |
24920 KB |
Output is correct |
36 |
Correct |
59 ms |
63316 KB |
Output is correct |
37 |
Correct |
75 ms |
79956 KB |
Output is correct |
38 |
Correct |
96 ms |
96512 KB |
Output is correct |
39 |
Correct |
163 ms |
164432 KB |
Output is correct |
40 |
Correct |
4 ms |
4952 KB |
Output is correct |
41 |
Correct |
6 ms |
7512 KB |
Output is correct |
42 |
Correct |
8 ms |
11992 KB |
Output is correct |
43 |
Correct |
123 ms |
63252 KB |
Output is correct |
44 |
Correct |
159 ms |
164436 KB |
Output is correct |
45 |
Correct |
28 ms |
32848 KB |
Output is correct |
46 |
Correct |
95 ms |
96336 KB |
Output is correct |
47 |
Correct |
173 ms |
164428 KB |
Output is correct |
48 |
Correct |
172 ms |
164692 KB |
Output is correct |
49 |
Correct |
148 ms |
163716 KB |
Output is correct |
50 |
Correct |
150 ms |
163664 KB |
Output is correct |