#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fore(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define fort(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define fordt(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define forde(i, a, b) for (int i = (a), _b = (b); i > _b; --i)
#define trav(a, x) for (auto& a : x)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
using namespace __gnu_pbds;
template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true):false;};
template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true):false;};
typedef int64_t i64;
typedef pair<int,int> _ii;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
constexpr int maxn=2e5+7;
constexpr i64 oo=1e9+7;
int n, a[5007], dp[5007][5007];
void solve() {
cin >> n;
fort(i, 1, n) cin >> a[i];
fort(mid, 1, n) {
fore(i, 1, mid)
maxi(dp[i][mid - 1], dp[i - 1][mid - 1]);
int r = mid, l = mid - 1, sum_up = a[r], sum_down = a[l], cnt = 0;
while(r < n || l > 1) {
if(r >= n) {
sum_down += a[--l];
maxi(dp[mid][r], dp[l][mid - 1] + cnt);
}
else if(l <= 1) {
sum_up += a[++r];
// maxi(dp[mid][r], dp[l][mid - 1] + cnt);
maxi(dp[mid][r], dp[mid][r - 1]);
}
else {
if(sum_up >= sum_down) {
sum_down += a[--l];
}
else {
sum_up += a[++r];
maxi(dp[mid][r], dp[mid][r - 1]);
}
// else {
// maxi(dp[mid][r], dp[l][mid - 1] + cnt);
// maxi(dp[mid][r + 1], dp[mid][r]);
// sum_up += a[++r];
// sum_down += a[--l];
// ++cnt;
// maxi(dp[mid][r], dp[l][mid - 1] + cnt);
// }
}
if(r - mid >= 1 && mid - l >= 2 && sum_down - a[l] == sum_up - a[r]) ++cnt;
maxi(dp[mid][r], dp[l][mid - 1] + cnt);
}
if(l > 1) maxi(dp[mid][r], dp[l - 1][mid - 1] + cnt);
}
int ans = 0;
fort(i, 1, n) maxi(ans, dp[i][n]);
cout << ans << '\n';
}
#define NAME "test."
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
if(fopen(NAME"inp", "r")) {
freopen(NAME"inp", "r", stdin);
freopen(NAME"out", "w", stdout);
}
int t=1;
while(t--) solve();
}
Compilation message
cigle.cpp: In function 'int main()':
cigle.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(NAME"inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
cigle.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(NAME"out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
604 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 |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
720 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2908 KB |
Output is correct |
2 |
Correct |
2 ms |
2776 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
3 ms |
2908 KB |
Output is correct |
7 |
Correct |
3 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 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 |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
720 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2776 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
3160 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
3 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2904 KB |
Output is correct |
40 |
Correct |
2 ms |
2384 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
2 ms |
2908 KB |
Output is correct |
43 |
Correct |
3 ms |
2652 KB |
Output is correct |
44 |
Correct |
2 ms |
2396 KB |
Output is correct |
45 |
Correct |
2 ms |
2652 KB |
Output is correct |
46 |
Correct |
2 ms |
2652 KB |
Output is correct |
47 |
Correct |
3 ms |
2652 KB |
Output is correct |
48 |
Correct |
2 ms |
2648 KB |
Output is correct |
49 |
Correct |
2 ms |
2652 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 |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
720 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2776 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
3 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
3160 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
3 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2904 KB |
Output is correct |
40 |
Correct |
2 ms |
2384 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
2 ms |
2908 KB |
Output is correct |
43 |
Correct |
3 ms |
2652 KB |
Output is correct |
44 |
Correct |
2 ms |
2396 KB |
Output is correct |
45 |
Correct |
2 ms |
2652 KB |
Output is correct |
46 |
Correct |
2 ms |
2652 KB |
Output is correct |
47 |
Correct |
3 ms |
2652 KB |
Output is correct |
48 |
Correct |
2 ms |
2648 KB |
Output is correct |
49 |
Correct |
2 ms |
2652 KB |
Output is correct |
50 |
Correct |
198 ms |
67164 KB |
Output is correct |
51 |
Correct |
193 ms |
67432 KB |
Output is correct |
52 |
Correct |
184 ms |
67152 KB |
Output is correct |
53 |
Correct |
186 ms |
67248 KB |
Output is correct |
54 |
Correct |
187 ms |
67140 KB |
Output is correct |
55 |
Correct |
180 ms |
67300 KB |
Output is correct |
56 |
Correct |
186 ms |
67084 KB |
Output is correct |
57 |
Correct |
187 ms |
67156 KB |
Output is correct |
58 |
Correct |
213 ms |
67188 KB |
Output is correct |
59 |
Correct |
196 ms |
66960 KB |
Output is correct |
60 |
Correct |
200 ms |
66388 KB |
Output is correct |
61 |
Correct |
189 ms |
67148 KB |
Output is correct |
62 |
Correct |
195 ms |
67244 KB |
Output is correct |
63 |
Correct |
180 ms |
67028 KB |
Output is correct |
64 |
Correct |
185 ms |
67040 KB |
Output is correct |
65 |
Correct |
183 ms |
66980 KB |
Output is correct |
66 |
Correct |
200 ms |
66528 KB |
Output is correct |
67 |
Correct |
202 ms |
66696 KB |
Output is correct |
68 |
Correct |
194 ms |
66512 KB |
Output is correct |
69 |
Correct |
185 ms |
66616 KB |
Output is correct |
70 |
Correct |
218 ms |
66908 KB |
Output is correct |
71 |
Correct |
202 ms |
67152 KB |
Output is correct |
72 |
Correct |
193 ms |
66916 KB |
Output is correct |