#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 + 1][j]);
// }
}
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++) {
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:45: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]
45 | 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
452 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 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 |
7 ms |
2008 KB |
Output is correct |
2 |
Correct |
8 ms |
2516 KB |
Output is correct |
3 |
Correct |
7 ms |
2512 KB |
Output is correct |
4 |
Correct |
7 ms |
2516 KB |
Output is correct |
5 |
Correct |
9 ms |
2516 KB |
Output is correct |
6 |
Correct |
10 ms |
2516 KB |
Output is correct |
7 |
Correct |
9 ms |
2516 KB |
Output is correct |
8 |
Correct |
8 ms |
2512 KB |
Output is correct |
9 |
Correct |
8 ms |
2516 KB |
Output is correct |
10 |
Correct |
6 ms |
2516 KB |
Output is correct |
11 |
Correct |
7 ms |
2516 KB |
Output is correct |
12 |
Correct |
7 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
452 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 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 |
7 ms |
2008 KB |
Output is correct |
22 |
Correct |
8 ms |
2516 KB |
Output is correct |
23 |
Correct |
7 ms |
2512 KB |
Output is correct |
24 |
Correct |
7 ms |
2516 KB |
Output is correct |
25 |
Correct |
9 ms |
2516 KB |
Output is correct |
26 |
Correct |
10 ms |
2516 KB |
Output is correct |
27 |
Correct |
9 ms |
2516 KB |
Output is correct |
28 |
Correct |
8 ms |
2512 KB |
Output is correct |
29 |
Correct |
8 ms |
2516 KB |
Output is correct |
30 |
Correct |
6 ms |
2516 KB |
Output is correct |
31 |
Correct |
7 ms |
2516 KB |
Output is correct |
32 |
Correct |
7 ms |
2516 KB |
Output is correct |
33 |
Correct |
6 ms |
2008 KB |
Output is correct |
34 |
Correct |
8 ms |
2516 KB |
Output is correct |
35 |
Correct |
8 ms |
2624 KB |
Output is correct |
36 |
Correct |
8 ms |
2512 KB |
Output is correct |
37 |
Correct |
8 ms |
2516 KB |
Output is correct |
38 |
Correct |
8 ms |
2516 KB |
Output is correct |
39 |
Correct |
3 ms |
1752 KB |
Output is correct |
40 |
Correct |
2 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
1372 KB |
Output is correct |
42 |
Correct |
2 ms |
1524 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 |
2 ms |
1516 KB |
Output is correct |
47 |
Correct |
1 ms |
1372 KB |
Output is correct |
48 |
Correct |
2 ms |
1372 KB |
Output is correct |
49 |
Correct |
2 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
452 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
452 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 |
7 ms |
2008 KB |
Output is correct |
22 |
Correct |
8 ms |
2516 KB |
Output is correct |
23 |
Correct |
7 ms |
2512 KB |
Output is correct |
24 |
Correct |
7 ms |
2516 KB |
Output is correct |
25 |
Correct |
9 ms |
2516 KB |
Output is correct |
26 |
Correct |
10 ms |
2516 KB |
Output is correct |
27 |
Correct |
9 ms |
2516 KB |
Output is correct |
28 |
Correct |
8 ms |
2512 KB |
Output is correct |
29 |
Correct |
8 ms |
2516 KB |
Output is correct |
30 |
Correct |
6 ms |
2516 KB |
Output is correct |
31 |
Correct |
7 ms |
2516 KB |
Output is correct |
32 |
Correct |
7 ms |
2516 KB |
Output is correct |
33 |
Correct |
6 ms |
2008 KB |
Output is correct |
34 |
Correct |
8 ms |
2516 KB |
Output is correct |
35 |
Correct |
8 ms |
2624 KB |
Output is correct |
36 |
Correct |
8 ms |
2512 KB |
Output is correct |
37 |
Correct |
8 ms |
2516 KB |
Output is correct |
38 |
Correct |
8 ms |
2516 KB |
Output is correct |
39 |
Correct |
3 ms |
1752 KB |
Output is correct |
40 |
Correct |
2 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
1372 KB |
Output is correct |
42 |
Correct |
2 ms |
1524 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 |
2 ms |
1516 KB |
Output is correct |
47 |
Correct |
1 ms |
1372 KB |
Output is correct |
48 |
Correct |
2 ms |
1372 KB |
Output is correct |
49 |
Correct |
2 ms |
1372 KB |
Output is correct |
50 |
Execution timed out |
1039 ms |
164536 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |