#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, ptr, cnt, ans = 0; cin >> n;
int arr[n], dp[n][n], pref[n][n], prefs[n + 1]; for (int i = 0; i < n; i++) cin >> arr[i];
prefs[0] = 0; for (int i = 0; i < n; i++) prefs[i + 1] = prefs[i] + arr[i];
for (int i = 1; i < n; i++) fill(dp[i], dp[i] + n, 0);
fill(dp[0], dp[0] + n, 0);
for (int i = 1; i < n; i++){
ptr = i - 1; cnt = 0;
for (int j = 0; j < n; j++){
if (!j) pref[i - 1][j] = dp[j][i - 1];
else pref[i - 1][j] = max(dp[j][i - 1], pref[i - 1][j - 1]);
}
for (int j = i; j < n; j++){
if (j == i) dp[i][j] = pref[i - 1][i - 1];
else{
dp[i][j] = dp[i][j - 1];
if (ptr >= 0 and prefs[i] - prefs[ptr + 1] == prefs[j] - prefs[i]){
if (prefs[i] - prefs[ptr + 1] == prefs[j] - prefs[i]) cnt += 1;
dp[i][j] = max(dp[i][j], pref[i - 1][ptr] + cnt);
}
while (ptr >= 0 and prefs[i] - prefs[ptr + 1] < prefs[j] - prefs[i]){
ptr--;
if (ptr >= 0 and prefs[i] - prefs[ptr + 1] == prefs[j] - prefs[i]){
if (prefs[i] - prefs[ptr + 1] == prefs[j] - prefs[i]) cnt += 1;
dp[i][j] = max(dp[i][j], pref[i - 1][ptr] + cnt);
}
}
}
}
}
for (int i = 0; i < n; i++) ans = max(ans, dp[i][n - 1]);
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 |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 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 |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
3 ms |
4180 KB |
Output is correct |
5 |
Correct |
3 ms |
4180 KB |
Output is correct |
6 |
Correct |
4 ms |
4296 KB |
Output is correct |
7 |
Correct |
5 ms |
4236 KB |
Output is correct |
8 |
Correct |
4 ms |
4180 KB |
Output is correct |
9 |
Correct |
4 ms |
4180 KB |
Output is correct |
10 |
Correct |
4 ms |
4180 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Correct |
4 ms |
4160 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 |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
4180 KB |
Output is correct |
22 |
Correct |
3 ms |
4180 KB |
Output is correct |
23 |
Correct |
3 ms |
4180 KB |
Output is correct |
24 |
Correct |
3 ms |
4180 KB |
Output is correct |
25 |
Correct |
3 ms |
4180 KB |
Output is correct |
26 |
Correct |
4 ms |
4296 KB |
Output is correct |
27 |
Correct |
5 ms |
4236 KB |
Output is correct |
28 |
Correct |
4 ms |
4180 KB |
Output is correct |
29 |
Correct |
4 ms |
4180 KB |
Output is correct |
30 |
Correct |
4 ms |
4180 KB |
Output is correct |
31 |
Correct |
3 ms |
4180 KB |
Output is correct |
32 |
Correct |
4 ms |
4160 KB |
Output is correct |
33 |
Correct |
3 ms |
4188 KB |
Output is correct |
34 |
Correct |
3 ms |
4180 KB |
Output is correct |
35 |
Correct |
3 ms |
4180 KB |
Output is correct |
36 |
Correct |
3 ms |
4180 KB |
Output is correct |
37 |
Correct |
3 ms |
4180 KB |
Output is correct |
38 |
Correct |
4 ms |
4180 KB |
Output is correct |
39 |
Correct |
4 ms |
4180 KB |
Output is correct |
40 |
Correct |
4 ms |
4180 KB |
Output is correct |
41 |
Correct |
3 ms |
4180 KB |
Output is correct |
42 |
Correct |
3 ms |
4156 KB |
Output is correct |
43 |
Correct |
3 ms |
4160 KB |
Output is correct |
44 |
Correct |
3 ms |
4180 KB |
Output is correct |
45 |
Correct |
3 ms |
4160 KB |
Output is correct |
46 |
Correct |
3 ms |
4180 KB |
Output is correct |
47 |
Correct |
5 ms |
4180 KB |
Output is correct |
48 |
Correct |
4 ms |
4180 KB |
Output is correct |
49 |
Correct |
3 ms |
4180 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 |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
4180 KB |
Output is correct |
22 |
Correct |
3 ms |
4180 KB |
Output is correct |
23 |
Correct |
3 ms |
4180 KB |
Output is correct |
24 |
Correct |
3 ms |
4180 KB |
Output is correct |
25 |
Correct |
3 ms |
4180 KB |
Output is correct |
26 |
Correct |
4 ms |
4296 KB |
Output is correct |
27 |
Correct |
5 ms |
4236 KB |
Output is correct |
28 |
Correct |
4 ms |
4180 KB |
Output is correct |
29 |
Correct |
4 ms |
4180 KB |
Output is correct |
30 |
Correct |
4 ms |
4180 KB |
Output is correct |
31 |
Correct |
3 ms |
4180 KB |
Output is correct |
32 |
Correct |
4 ms |
4160 KB |
Output is correct |
33 |
Correct |
3 ms |
4188 KB |
Output is correct |
34 |
Correct |
3 ms |
4180 KB |
Output is correct |
35 |
Correct |
3 ms |
4180 KB |
Output is correct |
36 |
Correct |
3 ms |
4180 KB |
Output is correct |
37 |
Correct |
3 ms |
4180 KB |
Output is correct |
38 |
Correct |
4 ms |
4180 KB |
Output is correct |
39 |
Correct |
4 ms |
4180 KB |
Output is correct |
40 |
Correct |
4 ms |
4180 KB |
Output is correct |
41 |
Correct |
3 ms |
4180 KB |
Output is correct |
42 |
Correct |
3 ms |
4156 KB |
Output is correct |
43 |
Correct |
3 ms |
4160 KB |
Output is correct |
44 |
Correct |
3 ms |
4180 KB |
Output is correct |
45 |
Correct |
3 ms |
4160 KB |
Output is correct |
46 |
Correct |
3 ms |
4180 KB |
Output is correct |
47 |
Correct |
5 ms |
4180 KB |
Output is correct |
48 |
Correct |
4 ms |
4180 KB |
Output is correct |
49 |
Correct |
3 ms |
4180 KB |
Output is correct |
50 |
Correct |
434 ms |
391776 KB |
Output is correct |
51 |
Correct |
470 ms |
391772 KB |
Output is correct |
52 |
Correct |
435 ms |
391776 KB |
Output is correct |
53 |
Correct |
454 ms |
391776 KB |
Output is correct |
54 |
Correct |
425 ms |
391776 KB |
Output is correct |
55 |
Correct |
424 ms |
391888 KB |
Output is correct |
56 |
Correct |
456 ms |
391776 KB |
Output is correct |
57 |
Correct |
464 ms |
391776 KB |
Output is correct |
58 |
Correct |
453 ms |
391772 KB |
Output is correct |
59 |
Correct |
453 ms |
391784 KB |
Output is correct |
60 |
Correct |
462 ms |
391872 KB |
Output is correct |
61 |
Correct |
426 ms |
391792 KB |
Output is correct |
62 |
Correct |
426 ms |
391788 KB |
Output is correct |
63 |
Correct |
417 ms |
391672 KB |
Output is correct |
64 |
Correct |
424 ms |
391784 KB |
Output is correct |
65 |
Correct |
420 ms |
391832 KB |
Output is correct |
66 |
Correct |
433 ms |
391712 KB |
Output is correct |
67 |
Correct |
415 ms |
391780 KB |
Output is correct |
68 |
Correct |
418 ms |
391708 KB |
Output is correct |
69 |
Correct |
416 ms |
391816 KB |
Output is correct |
70 |
Correct |
437 ms |
391752 KB |
Output is correct |
71 |
Correct |
454 ms |
391756 KB |
Output is correct |
72 |
Correct |
441 ms |
391784 KB |
Output is correct |