// QuangBuiCP
#ifndef LOCAL
#pragma GCC optimize(3)
#pragma GCC target("avx2")
#endif // LOCAL
#include "bits/stdc++.h"
using namespace std;
#define SZ(a) (int)(a).size()
#define ALL(a) (a).begin(),(a).end()
const int N = 5005;
int n;
int a[N];
int pref[N];
int dp[N][N];
int f[N];
int Brute() {
int ret = 0;
for (int mask = 0; mask < (1 << n); ++mask) {
int cnt = 0;
set<int> prv, cur;
int pos = 0, layer = 0;
bool no = false;
for (int i = 1; i < n; ++i) {
int x1 = 0, x2 = 0;
if (mask >> i & 1) {
x1 = 1;
}
if (mask >> (i - 1) & 1) {
x2 = 1;
}
if (x1 && x2) {
no = true;
break;
}
}
if (no) {
continue;
}
for (int i = 0; i < n; ++i) {
if (mask >> i & 1) {
layer++;
prv = cur;
cur.clear();
}
int sign = 1;
if (layer % 2) {
sign *= -1;
}
pos += sign * a[i + 1];
if (prv.count(pos) && i != n - 1 && !(mask >> (i + 1) & 1)) {
cnt++;
}
cur.insert(pos);
}
ret = max(ret, cnt);
}
return ret;
}
signed main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif // LOCAL
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
}
// cout << Brute() << '\n';
for (int j = 1; j <= n; ++j) {
for (int i = 0; i < j; ++i) {
f[i] = dp[i][j];
}
for (int i = 1; i < j; ++i) {
f[i] = max(f[i], f[i - 1]);
}
int cur = 0, cnt = 0;
for (int i = j - 1, k = j + 1; k <= n; ++k) {
dp[j][k] = cur;
// [i + 1, j] == [j + 1, k]
while (i > 0 && a[i] + a[k] > a[j] * 2) {
i--;
}
if (i > 0 && a[i] + a[k] == a[j] * 2) {
cnt++;
cur = max(cur, f[i - 1] + cnt);
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
ans = max(ans, dp[i][n]);
}
cout << ans << '\n';
#ifdef LOCAL
cerr << '\n' << clock() << "ms.";
#endif // LOCAL
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2448 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2540 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2448 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2540 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10772 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2448 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2540 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10772 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
1 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10764 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10592 KB |
Output is correct |
39 |
Correct |
2 ms |
10584 KB |
Output is correct |
40 |
Correct |
2 ms |
10604 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10592 KB |
Output is correct |
45 |
Correct |
2 ms |
10592 KB |
Output is correct |
46 |
Correct |
2 ms |
10596 KB |
Output is correct |
47 |
Correct |
2 ms |
10588 KB |
Output is correct |
48 |
Correct |
3 ms |
10588 KB |
Output is correct |
49 |
Correct |
2 ms |
10764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2448 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2540 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10772 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
1 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10764 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10592 KB |
Output is correct |
39 |
Correct |
2 ms |
10584 KB |
Output is correct |
40 |
Correct |
2 ms |
10604 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10592 KB |
Output is correct |
45 |
Correct |
2 ms |
10592 KB |
Output is correct |
46 |
Correct |
2 ms |
10596 KB |
Output is correct |
47 |
Correct |
2 ms |
10588 KB |
Output is correct |
48 |
Correct |
3 ms |
10588 KB |
Output is correct |
49 |
Correct |
2 ms |
10764 KB |
Output is correct |
50 |
Correct |
46 ms |
97144 KB |
Output is correct |
51 |
Correct |
42 ms |
97228 KB |
Output is correct |
52 |
Correct |
47 ms |
97160 KB |
Output is correct |
53 |
Correct |
47 ms |
97108 KB |
Output is correct |
54 |
Correct |
47 ms |
97236 KB |
Output is correct |
55 |
Correct |
46 ms |
97484 KB |
Output is correct |
56 |
Correct |
84 ms |
97388 KB |
Output is correct |
57 |
Correct |
78 ms |
97216 KB |
Output is correct |
58 |
Correct |
83 ms |
97104 KB |
Output is correct |
59 |
Correct |
80 ms |
97104 KB |
Output is correct |
60 |
Correct |
80 ms |
97200 KB |
Output is correct |
61 |
Correct |
66 ms |
97232 KB |
Output is correct |
62 |
Correct |
60 ms |
97156 KB |
Output is correct |
63 |
Correct |
55 ms |
97360 KB |
Output is correct |
64 |
Correct |
61 ms |
97104 KB |
Output is correct |
65 |
Correct |
58 ms |
97212 KB |
Output is correct |
66 |
Correct |
69 ms |
97252 KB |
Output is correct |
67 |
Correct |
52 ms |
97200 KB |
Output is correct |
68 |
Correct |
50 ms |
97108 KB |
Output is correct |
69 |
Correct |
48 ms |
97104 KB |
Output is correct |
70 |
Correct |
75 ms |
97108 KB |
Output is correct |
71 |
Correct |
80 ms |
97252 KB |
Output is correct |
72 |
Correct |
70 ms |
97268 KB |
Output is correct |