#include <bits/stdc++.h>
int32_t main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> input(n);
for (auto& i : input) {
std::cin >> i;
}
std::vector<int> prf(n + 1);
for (int i = 0; i < n; i++) {
prf[i + 1] = prf[i] + input[i];
}
std::vector<std::vector<int>> dp(n, std::vector<int>(n));
std::vector<std::array<int, 4>> vec;
for (int i = 1; i < n; i++) {
int dlt = 0;
int sum = 0;
for (int j = i, j2 = i; j < n; j++) {
sum += input[j];
while (j2 > 0 && prf[i] - prf[j2] < sum) {
j2--;
}
if (j2 > 0 && j + 1 < n && prf[i] - prf[j2] == sum) {
dlt++;
vec.push_back({j2 - 1, i, j + 1, dlt});
}
}
}
std::sort(vec.begin(), vec.end(), [&](auto a, auto b) { return a[1] < b[1]; });
int vec_ptr = 0;
for (int j = 0; j < n; j++) {
for (int i = 0; i <= j; i++) {
if (i > 0) {
dp[i][j] = std::max(dp[i][j], dp[i - 1][j]);
}
if (i < j) {
dp[i][j] = std::max(dp[i][j], dp[i][j - 1]);
}
}
while (vec_ptr < vec.size() && vec[vec_ptr][1] == j) {
auto [a, b, c, dlt] = vec[vec_ptr++];
// for (int t = 0; c + t < n; t++) {
for (int t = 0; t < 1; t++) {
dp[b][c + t] = std::max(dp[b][c + t], dp[a][b - 1] + dlt);
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans = std::max(ans, dp[i][j]);
}
}
std::cout << ans << "\n";
return 0;
}
Compilation message
cigle.cpp: In function 'int32_t main()':
cigle.cpp:46:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | while (vec_ptr < vec.size() && vec[vec_ptr][1] == j) {
| ~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2008 KB |
Output is correct |
2 |
Correct |
3 ms |
2516 KB |
Output is correct |
3 |
Correct |
3 ms |
2516 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
5 |
Correct |
3 ms |
2516 KB |
Output is correct |
6 |
Correct |
3 ms |
2516 KB |
Output is correct |
7 |
Correct |
3 ms |
2516 KB |
Output is correct |
8 |
Correct |
3 ms |
2516 KB |
Output is correct |
9 |
Correct |
3 ms |
2396 KB |
Output is correct |
10 |
Correct |
3 ms |
2512 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
3 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
2008 KB |
Output is correct |
22 |
Correct |
3 ms |
2516 KB |
Output is correct |
23 |
Correct |
3 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
3 ms |
2516 KB |
Output is correct |
27 |
Correct |
3 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2516 KB |
Output is correct |
29 |
Correct |
3 ms |
2396 KB |
Output is correct |
30 |
Correct |
3 ms |
2512 KB |
Output is correct |
31 |
Correct |
3 ms |
2516 KB |
Output is correct |
32 |
Correct |
3 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
2004 KB |
Output is correct |
34 |
Correct |
3 ms |
2516 KB |
Output is correct |
35 |
Correct |
3 ms |
2516 KB |
Output is correct |
36 |
Correct |
3 ms |
2516 KB |
Output is correct |
37 |
Correct |
4 ms |
2516 KB |
Output is correct |
38 |
Correct |
5 ms |
2516 KB |
Output is correct |
39 |
Correct |
2 ms |
1752 KB |
Output is correct |
40 |
Correct |
1 ms |
1372 KB |
Output is correct |
41 |
Correct |
1 ms |
1372 KB |
Output is correct |
42 |
Correct |
2 ms |
1372 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1372 KB |
Output is correct |
46 |
Correct |
3 ms |
1628 KB |
Output is correct |
47 |
Correct |
1 ms |
1372 KB |
Output is correct |
48 |
Correct |
2 ms |
1372 KB |
Output is correct |
49 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
2008 KB |
Output is correct |
22 |
Correct |
3 ms |
2516 KB |
Output is correct |
23 |
Correct |
3 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
3 ms |
2516 KB |
Output is correct |
27 |
Correct |
3 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2516 KB |
Output is correct |
29 |
Correct |
3 ms |
2396 KB |
Output is correct |
30 |
Correct |
3 ms |
2512 KB |
Output is correct |
31 |
Correct |
3 ms |
2516 KB |
Output is correct |
32 |
Correct |
3 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
2004 KB |
Output is correct |
34 |
Correct |
3 ms |
2516 KB |
Output is correct |
35 |
Correct |
3 ms |
2516 KB |
Output is correct |
36 |
Correct |
3 ms |
2516 KB |
Output is correct |
37 |
Correct |
4 ms |
2516 KB |
Output is correct |
38 |
Correct |
5 ms |
2516 KB |
Output is correct |
39 |
Correct |
2 ms |
1752 KB |
Output is correct |
40 |
Correct |
1 ms |
1372 KB |
Output is correct |
41 |
Correct |
1 ms |
1372 KB |
Output is correct |
42 |
Correct |
2 ms |
1372 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1372 KB |
Output is correct |
46 |
Correct |
3 ms |
1628 KB |
Output is correct |
47 |
Correct |
1 ms |
1372 KB |
Output is correct |
48 |
Correct |
2 ms |
1372 KB |
Output is correct |
49 |
Correct |
1 ms |
1372 KB |
Output is correct |
50 |
Correct |
275 ms |
164468 KB |
Output is correct |
51 |
Correct |
270 ms |
164288 KB |
Output is correct |
52 |
Correct |
277 ms |
164792 KB |
Output is correct |
53 |
Correct |
286 ms |
165880 KB |
Output is correct |
54 |
Correct |
289 ms |
165808 KB |
Output is correct |
55 |
Correct |
279 ms |
166080 KB |
Output is correct |
56 |
Correct |
307 ms |
165832 KB |
Output is correct |
57 |
Correct |
310 ms |
164792 KB |
Output is correct |
58 |
Correct |
228 ms |
131524 KB |
Output is correct |
59 |
Correct |
195 ms |
103124 KB |
Output is correct |
60 |
Correct |
186 ms |
98624 KB |
Output is correct |
61 |
Correct |
170 ms |
98896 KB |
Output is correct |
62 |
Correct |
168 ms |
98896 KB |
Output is correct |
63 |
Correct |
167 ms |
99704 KB |
Output is correct |
64 |
Correct |
164 ms |
99700 KB |
Output is correct |
65 |
Correct |
161 ms |
99716 KB |
Output is correct |
66 |
Correct |
165 ms |
98988 KB |
Output is correct |
67 |
Correct |
163 ms |
99560 KB |
Output is correct |
68 |
Correct |
154 ms |
99588 KB |
Output is correct |
69 |
Correct |
187 ms |
99548 KB |
Output is correct |
70 |
Correct |
187 ms |
98896 KB |
Output is correct |
71 |
Correct |
196 ms |
98904 KB |
Output is correct |
72 |
Correct |
182 ms |
99636 KB |
Output is correct |