#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000009
#define LOGN 20
#define MAXN 500005
#define int long long
int n;
int sg[4*MAXN];
vector<int> v, pref;
int get_sum(int begin, int len) {
if (begin + len - 1 <= n)
return pref[begin + len - 1] - pref[begin - 1];
else
return pref[n] - pref[begin - 1] + pref[len - n + begin - 1];
}
void init(int k, int a, int b) {
if (a == b) {
sg[k] = get_sum(a, n/2);
return ;
}
init(2*k, a, (a+b)/2);
init(2*k+1, (a+b)/2+1, b);
sg[k] = max(sg[2*k], sg[2*k+1]);
}
int query(int k, int a, int b, int q_l, int q_r) {
if (a > q_r || b < q_l)
return 0;
if (q_l <= a && b <= q_r)
return sg[k];
return max(query(2*k, a, (a+b)/2, q_l, q_r), query(2*k+1, (a+b)/2+1, b, q_l, q_r));
}
signed main() {
fast
cin >> n;
v = vector<int>(n+1);
pref = vector<int>(n+1, 0);
for (int i = 1; i <= n; i++) {
cin >> v[i];
pref[i] = pref[i-1] + v[i];
}
init(1, 1, n);
int ans = 0;
for (int i = 1; i <= n; i++) {
int max_protect;
if (n % 2 && i + 1 + n/2 <= n)
max_protect = query(1, 1, n, i + 1, i + 1 + n/2);
else if (n % 2 == 0 && i + n/2 <= n)
max_protect = query(1, 1, n, i + 1, i + n/2);
else if (n % 2)
max_protect = max(query(1, 1, n, i + 1, n), query(1, 1, n, 1, 1 + i + n/2 - n));
else
max_protect = max(query(1, 1, n, i + 1, n), query(1, 1, n, 1, i - n/2));
ans = max(ans, pref[n] - max_protect);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
476 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
2 ms |
536 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
23 ms |
3656 KB |
Output is correct |
5 |
Correct |
57 ms |
7508 KB |
Output is correct |
6 |
Correct |
74 ms |
8348 KB |
Output is correct |
7 |
Correct |
93 ms |
13232 KB |
Output is correct |
8 |
Correct |
168 ms |
16364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
476 KB |
Output is correct |
24 |
Correct |
2 ms |
344 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
2 ms |
536 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
23 ms |
3656 KB |
Output is correct |
36 |
Correct |
57 ms |
7508 KB |
Output is correct |
37 |
Correct |
74 ms |
8348 KB |
Output is correct |
38 |
Correct |
93 ms |
13232 KB |
Output is correct |
39 |
Correct |
168 ms |
16364 KB |
Output is correct |
40 |
Correct |
3 ms |
792 KB |
Output is correct |
41 |
Correct |
6 ms |
1236 KB |
Output is correct |
42 |
Correct |
9 ms |
1476 KB |
Output is correct |
43 |
Correct |
60 ms |
8404 KB |
Output is correct |
44 |
Correct |
157 ms |
18544 KB |
Output is correct |
45 |
Correct |
28 ms |
4180 KB |
Output is correct |
46 |
Correct |
91 ms |
14544 KB |
Output is correct |
47 |
Correct |
159 ms |
18544 KB |
Output is correct |
48 |
Correct |
160 ms |
18816 KB |
Output is correct |
49 |
Correct |
152 ms |
17944 KB |
Output is correct |
50 |
Correct |
158 ms |
17944 KB |
Output is correct |