/*input
5
1 1 1 1 1
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N 500005
int n;
int a[N];
int sum[N];
int f[4 * N];
int getsum(int l, int r) {
return sum[r] - sum[l - 1];
}
int ceil(int x, int y) {
int ret = x / y;
if (x % y) ret++;
return ret;
}
void update(int k, int l, int r, int pos, int val) {
if (pos < l || r < pos) return;
if (l == r) {
f[k] = val;
return;
}
int mid = (l + r) / 2;
update(k * 2, l, mid, pos, val); update(k * 2 + 1, mid + 1, r, pos, val);
f[k] = max(f[k * 2], f[k * 2 + 1]);
}
int get(int k, int l, int r, int L, int R) {
if (r < L || R < l) return 0;
if (L <= l && r <= R) return f[k];
int mid = (l + r) / 2;
return max(get(k * 2, l, mid, L, R), get(k * 2 + 1, mid + 1, r, L, R));
}
// https://oj.uz/problem/view/BOI15_hac
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
int ans = 0;
for (int i = 1; i <= n; i++) {
int last = i + n / 2 - 1;
if (last <= n) {
update(1, 1, n, i, getsum(i, last));
}
else {
update(1, 1, n, i, getsum(i, n) + getsum(1, n / 2 - (n - i + 1)));
}
}
for (int i = 1; i <= n; i++) {
int last = i - n / 2;
if (last < 0) last += n;
int res = 0;
if (last < i) {
if (i != n)
res = get(1, 1, n, i + 1, n);
res = max(res, get(1, 1, n, 1, last));
}
else {
res = get(1, 1, n, i + 1, last);
}
ans = max(ans, sum[n] - res);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25612 KB |
Output is correct |
2 |
Correct |
0 ms |
25612 KB |
Output is correct |
3 |
Correct |
0 ms |
25612 KB |
Output is correct |
4 |
Correct |
0 ms |
25612 KB |
Output is correct |
5 |
Correct |
0 ms |
25612 KB |
Output is correct |
6 |
Correct |
0 ms |
25612 KB |
Output is correct |
7 |
Correct |
0 ms |
25612 KB |
Output is correct |
8 |
Correct |
0 ms |
25612 KB |
Output is correct |
9 |
Correct |
0 ms |
25612 KB |
Output is correct |
10 |
Correct |
0 ms |
25612 KB |
Output is correct |
11 |
Correct |
0 ms |
25612 KB |
Output is correct |
12 |
Correct |
0 ms |
25612 KB |
Output is correct |
13 |
Correct |
0 ms |
25612 KB |
Output is correct |
14 |
Correct |
0 ms |
25612 KB |
Output is correct |
15 |
Correct |
0 ms |
25612 KB |
Output is correct |
16 |
Correct |
0 ms |
25612 KB |
Output is correct |
17 |
Correct |
0 ms |
25612 KB |
Output is correct |
18 |
Correct |
0 ms |
25612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25612 KB |
Output is correct |
2 |
Correct |
0 ms |
25612 KB |
Output is correct |
3 |
Correct |
0 ms |
25612 KB |
Output is correct |
4 |
Correct |
0 ms |
25612 KB |
Output is correct |
5 |
Correct |
0 ms |
25612 KB |
Output is correct |
6 |
Correct |
0 ms |
25612 KB |
Output is correct |
7 |
Correct |
0 ms |
25612 KB |
Output is correct |
8 |
Correct |
0 ms |
25612 KB |
Output is correct |
9 |
Correct |
0 ms |
25612 KB |
Output is correct |
10 |
Correct |
0 ms |
25612 KB |
Output is correct |
11 |
Correct |
0 ms |
25612 KB |
Output is correct |
12 |
Correct |
0 ms |
25612 KB |
Output is correct |
13 |
Correct |
0 ms |
25612 KB |
Output is correct |
14 |
Correct |
0 ms |
25612 KB |
Output is correct |
15 |
Correct |
0 ms |
25612 KB |
Output is correct |
16 |
Correct |
0 ms |
25612 KB |
Output is correct |
17 |
Correct |
0 ms |
25612 KB |
Output is correct |
18 |
Correct |
0 ms |
25612 KB |
Output is correct |
19 |
Correct |
0 ms |
25612 KB |
Output is correct |
20 |
Correct |
0 ms |
25612 KB |
Output is correct |
21 |
Correct |
0 ms |
25612 KB |
Output is correct |
22 |
Correct |
0 ms |
25612 KB |
Output is correct |
23 |
Correct |
0 ms |
25612 KB |
Output is correct |
24 |
Correct |
0 ms |
25612 KB |
Output is correct |
25 |
Correct |
0 ms |
25612 KB |
Output is correct |
26 |
Correct |
0 ms |
25612 KB |
Output is correct |
27 |
Correct |
0 ms |
25612 KB |
Output is correct |
28 |
Correct |
0 ms |
25612 KB |
Output is correct |
29 |
Correct |
0 ms |
25612 KB |
Output is correct |
30 |
Correct |
3 ms |
25612 KB |
Output is correct |
31 |
Correct |
3 ms |
25612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25612 KB |
Output is correct |
2 |
Correct |
0 ms |
25612 KB |
Output is correct |
3 |
Correct |
3 ms |
25612 KB |
Output is correct |
4 |
Correct |
49 ms |
25612 KB |
Output is correct |
5 |
Correct |
126 ms |
25612 KB |
Output is correct |
6 |
Correct |
156 ms |
25612 KB |
Output is correct |
7 |
Correct |
196 ms |
25612 KB |
Output is correct |
8 |
Correct |
346 ms |
25612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
25612 KB |
Output is correct |
2 |
Correct |
0 ms |
25612 KB |
Output is correct |
3 |
Correct |
0 ms |
25612 KB |
Output is correct |
4 |
Correct |
0 ms |
25612 KB |
Output is correct |
5 |
Correct |
0 ms |
25612 KB |
Output is correct |
6 |
Correct |
0 ms |
25612 KB |
Output is correct |
7 |
Correct |
0 ms |
25612 KB |
Output is correct |
8 |
Correct |
0 ms |
25612 KB |
Output is correct |
9 |
Correct |
0 ms |
25612 KB |
Output is correct |
10 |
Correct |
0 ms |
25612 KB |
Output is correct |
11 |
Correct |
0 ms |
25612 KB |
Output is correct |
12 |
Correct |
0 ms |
25612 KB |
Output is correct |
13 |
Correct |
0 ms |
25612 KB |
Output is correct |
14 |
Correct |
0 ms |
25612 KB |
Output is correct |
15 |
Correct |
0 ms |
25612 KB |
Output is correct |
16 |
Correct |
0 ms |
25612 KB |
Output is correct |
17 |
Correct |
0 ms |
25612 KB |
Output is correct |
18 |
Correct |
0 ms |
25612 KB |
Output is correct |
19 |
Correct |
0 ms |
25612 KB |
Output is correct |
20 |
Correct |
0 ms |
25612 KB |
Output is correct |
21 |
Correct |
0 ms |
25612 KB |
Output is correct |
22 |
Correct |
0 ms |
25612 KB |
Output is correct |
23 |
Correct |
0 ms |
25612 KB |
Output is correct |
24 |
Correct |
0 ms |
25612 KB |
Output is correct |
25 |
Correct |
0 ms |
25612 KB |
Output is correct |
26 |
Correct |
0 ms |
25612 KB |
Output is correct |
27 |
Correct |
0 ms |
25612 KB |
Output is correct |
28 |
Correct |
0 ms |
25612 KB |
Output is correct |
29 |
Correct |
0 ms |
25612 KB |
Output is correct |
30 |
Correct |
3 ms |
25612 KB |
Output is correct |
31 |
Correct |
3 ms |
25612 KB |
Output is correct |
32 |
Correct |
0 ms |
25612 KB |
Output is correct |
33 |
Correct |
0 ms |
25612 KB |
Output is correct |
34 |
Correct |
3 ms |
25612 KB |
Output is correct |
35 |
Correct |
49 ms |
25612 KB |
Output is correct |
36 |
Correct |
126 ms |
25612 KB |
Output is correct |
37 |
Correct |
156 ms |
25612 KB |
Output is correct |
38 |
Correct |
196 ms |
25612 KB |
Output is correct |
39 |
Correct |
346 ms |
25612 KB |
Output is correct |
40 |
Correct |
3 ms |
25612 KB |
Output is correct |
41 |
Correct |
9 ms |
25612 KB |
Output is correct |
42 |
Correct |
16 ms |
25612 KB |
Output is correct |
43 |
Correct |
129 ms |
25612 KB |
Output is correct |
44 |
Correct |
326 ms |
25612 KB |
Output is correct |
45 |
Correct |
56 ms |
25612 KB |
Output is correct |
46 |
Correct |
186 ms |
25612 KB |
Output is correct |
47 |
Correct |
319 ms |
25612 KB |
Output is correct |
48 |
Correct |
333 ms |
25612 KB |
Output is correct |
49 |
Correct |
309 ms |
25612 KB |
Output is correct |
50 |
Correct |
349 ms |
25612 KB |
Output is correct |