#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<int> d(n);
for (int &x : d) {
cin >> x;
}
vector<vector<int>> f(n, vector<int>(n));
for (int i = 0; i < n - 1; i++) {
vector<int> num(n);
int l = i, r = i + 1, s = 0, cnt = 0;
while (~l || r < n) {
if (r == n || s <= 0 && l >= 0) s += d[l--];
else s -= d[r++];
if (!s) cnt++;
if (~l) num[l] = cnt;
if (r < n) num[r] = cnt;
}
vector<int> pre(i + 1);
for (int j = 0; j <= i; j++) {
pre[j] = max(j ? pre[j - 1] : 0, f[j][i]);
}
for (int k = i + 1, j = i, t = 0; k < n; k++) {
for (; ~j && num[j] <= num[k]; j--) {
t = max(t, f[j][i] + num[j]);
}
f[i + 1][k] = max(t, ~j ? pre[j] + num[k] : 0);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, f[i][n - 1]);
}
cout << ans << "\n";
return 0;
}
Compilation message
cigle.cpp: In function 'int main()':
cigle.cpp:17:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
17 | if (r == n || s <= 0 && l >= 0) s += d[l--];
| ~~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
324 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1344 KB |
Output is correct |
4 |
Correct |
3 ms |
1364 KB |
Output is correct |
5 |
Correct |
2 ms |
1236 KB |
Output is correct |
6 |
Correct |
3 ms |
1344 KB |
Output is correct |
7 |
Correct |
2 ms |
1236 KB |
Output is correct |
8 |
Correct |
3 ms |
1236 KB |
Output is correct |
9 |
Correct |
2 ms |
1236 KB |
Output is correct |
10 |
Correct |
2 ms |
1348 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
324 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1344 KB |
Output is correct |
24 |
Correct |
3 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1236 KB |
Output is correct |
26 |
Correct |
3 ms |
1344 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
3 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1348 KB |
Output is correct |
31 |
Correct |
2 ms |
1236 KB |
Output is correct |
32 |
Correct |
2 ms |
1236 KB |
Output is correct |
33 |
Correct |
2 ms |
1344 KB |
Output is correct |
34 |
Correct |
2 ms |
1236 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
2 ms |
1236 KB |
Output is correct |
37 |
Correct |
2 ms |
1236 KB |
Output is correct |
38 |
Correct |
3 ms |
1340 KB |
Output is correct |
39 |
Correct |
2 ms |
1236 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
2 ms |
1236 KB |
Output is correct |
43 |
Correct |
2 ms |
1348 KB |
Output is correct |
44 |
Correct |
2 ms |
1344 KB |
Output is correct |
45 |
Correct |
2 ms |
1236 KB |
Output is correct |
46 |
Correct |
2 ms |
1236 KB |
Output is correct |
47 |
Correct |
2 ms |
1236 KB |
Output is correct |
48 |
Correct |
2 ms |
1236 KB |
Output is correct |
49 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
324 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1344 KB |
Output is correct |
24 |
Correct |
3 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1236 KB |
Output is correct |
26 |
Correct |
3 ms |
1344 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
3 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1348 KB |
Output is correct |
31 |
Correct |
2 ms |
1236 KB |
Output is correct |
32 |
Correct |
2 ms |
1236 KB |
Output is correct |
33 |
Correct |
2 ms |
1344 KB |
Output is correct |
34 |
Correct |
2 ms |
1236 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
2 ms |
1236 KB |
Output is correct |
37 |
Correct |
2 ms |
1236 KB |
Output is correct |
38 |
Correct |
3 ms |
1340 KB |
Output is correct |
39 |
Correct |
2 ms |
1236 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
2 ms |
1236 KB |
Output is correct |
42 |
Correct |
2 ms |
1236 KB |
Output is correct |
43 |
Correct |
2 ms |
1348 KB |
Output is correct |
44 |
Correct |
2 ms |
1344 KB |
Output is correct |
45 |
Correct |
2 ms |
1236 KB |
Output is correct |
46 |
Correct |
2 ms |
1236 KB |
Output is correct |
47 |
Correct |
2 ms |
1236 KB |
Output is correct |
48 |
Correct |
2 ms |
1236 KB |
Output is correct |
49 |
Correct |
2 ms |
1236 KB |
Output is correct |
50 |
Correct |
221 ms |
98536 KB |
Output is correct |
51 |
Correct |
237 ms |
98440 KB |
Output is correct |
52 |
Correct |
221 ms |
98508 KB |
Output is correct |
53 |
Correct |
213 ms |
98672 KB |
Output is correct |
54 |
Correct |
223 ms |
98440 KB |
Output is correct |
55 |
Correct |
212 ms |
98636 KB |
Output is correct |
56 |
Correct |
260 ms |
98424 KB |
Output is correct |
57 |
Correct |
250 ms |
98436 KB |
Output is correct |
58 |
Correct |
297 ms |
98432 KB |
Output is correct |
59 |
Correct |
266 ms |
98452 KB |
Output is correct |
60 |
Correct |
254 ms |
98456 KB |
Output is correct |
61 |
Correct |
270 ms |
98536 KB |
Output is correct |
62 |
Correct |
231 ms |
98444 KB |
Output is correct |
63 |
Correct |
234 ms |
98572 KB |
Output is correct |
64 |
Correct |
240 ms |
98456 KB |
Output is correct |
65 |
Correct |
241 ms |
98572 KB |
Output is correct |
66 |
Correct |
238 ms |
98392 KB |
Output is correct |
67 |
Correct |
219 ms |
98380 KB |
Output is correct |
68 |
Correct |
226 ms |
98456 KB |
Output is correct |
69 |
Correct |
227 ms |
98460 KB |
Output is correct |
70 |
Correct |
248 ms |
98496 KB |
Output is correct |
71 |
Correct |
261 ms |
98536 KB |
Output is correct |
72 |
Correct |
240 ms |
98440 KB |
Output is correct |