#include <bits/stdc++.h>
using namespace std;
const int MX = 5005;
void chmax(int& a, int b){
if(b > a) a = b;
}
int N, arr[MX], dp[MX][MX];
// Observation #1 : We can easily solve this in O(N^3)
// dp(i, j) is the largest beauty of the wall if the highest row includes [i, j]
// the transition is we add another row [j + 1, k] for every j + 1 <= k <= N
// calculating is easy by two pointer
// Observation #2 : Now we can optimize the first solution so that it becomes O(N^2)
// if we place j at a certain position
// and have two pointers (i, k)
// we will see that the beauty will increase a total of O(N) times
// So we can find the positions where the value changes in O(N) for each j
// update those positions
// and after we update max the current value with the previous value
// Note : To make finding range query faster use prefix sum (because l = 0)
struct prefix{
int v[MX];
void build(int p){
for(int i = 0; i <= p; i++) v[i] = i == 0 ? dp[i][p] : max(dp[i][p], v[i - 1]);
}
} ps;
int main(){
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
cin >> N;
for(int i = 0; i < N; i++) cin >> arr[i];
for(int p = 0; p < N; p++){
int i = p, k = p + 1, cnt = -1, bot = 0, top = 0;
ps.build(p);
while(i >= 0 && k < N){
if(bot == top){
cnt++; chmax(dp[p + 1][k], ps.v[i] + cnt);
bot += arr[i--]; top += arr[k++];
}else if(bot < top){
bot += arr[i--];
}else if(top < bot){
top += arr[k++];
}
}
for(int j = p + 1; j < N; j++) chmax(dp[p + 1][j], dp[p + 1][j - 1]);
}
int ans = 0;
for(int p = 0; p < N; p++) chmax(ans, dp[p][N - 1]);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2764 KB |
Output is correct |
2 |
Correct |
3 ms |
2764 KB |
Output is correct |
3 |
Correct |
3 ms |
2764 KB |
Output is correct |
4 |
Correct |
4 ms |
2764 KB |
Output is correct |
5 |
Correct |
4 ms |
2764 KB |
Output is correct |
6 |
Correct |
4 ms |
2764 KB |
Output is correct |
7 |
Correct |
4 ms |
2764 KB |
Output is correct |
8 |
Correct |
4 ms |
2764 KB |
Output is correct |
9 |
Correct |
4 ms |
2764 KB |
Output is correct |
10 |
Correct |
3 ms |
2764 KB |
Output is correct |
11 |
Correct |
4 ms |
2764 KB |
Output is correct |
12 |
Correct |
3 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
3 ms |
2764 KB |
Output is correct |
22 |
Correct |
3 ms |
2764 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
4 ms |
2764 KB |
Output is correct |
25 |
Correct |
4 ms |
2764 KB |
Output is correct |
26 |
Correct |
4 ms |
2764 KB |
Output is correct |
27 |
Correct |
4 ms |
2764 KB |
Output is correct |
28 |
Correct |
4 ms |
2764 KB |
Output is correct |
29 |
Correct |
4 ms |
2764 KB |
Output is correct |
30 |
Correct |
3 ms |
2764 KB |
Output is correct |
31 |
Correct |
4 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
3 ms |
2764 KB |
Output is correct |
34 |
Correct |
3 ms |
2764 KB |
Output is correct |
35 |
Correct |
3 ms |
2764 KB |
Output is correct |
36 |
Correct |
3 ms |
2764 KB |
Output is correct |
37 |
Correct |
4 ms |
2764 KB |
Output is correct |
38 |
Correct |
4 ms |
2764 KB |
Output is correct |
39 |
Correct |
4 ms |
2764 KB |
Output is correct |
40 |
Correct |
3 ms |
2028 KB |
Output is correct |
41 |
Correct |
3 ms |
2764 KB |
Output is correct |
42 |
Correct |
3 ms |
2764 KB |
Output is correct |
43 |
Correct |
3 ms |
2636 KB |
Output is correct |
44 |
Correct |
3 ms |
2252 KB |
Output is correct |
45 |
Correct |
3 ms |
2636 KB |
Output is correct |
46 |
Correct |
3 ms |
2636 KB |
Output is correct |
47 |
Correct |
3 ms |
2636 KB |
Output is correct |
48 |
Correct |
3 ms |
2636 KB |
Output is correct |
49 |
Correct |
3 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
3 ms |
2764 KB |
Output is correct |
22 |
Correct |
3 ms |
2764 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
4 ms |
2764 KB |
Output is correct |
25 |
Correct |
4 ms |
2764 KB |
Output is correct |
26 |
Correct |
4 ms |
2764 KB |
Output is correct |
27 |
Correct |
4 ms |
2764 KB |
Output is correct |
28 |
Correct |
4 ms |
2764 KB |
Output is correct |
29 |
Correct |
4 ms |
2764 KB |
Output is correct |
30 |
Correct |
3 ms |
2764 KB |
Output is correct |
31 |
Correct |
4 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
3 ms |
2764 KB |
Output is correct |
34 |
Correct |
3 ms |
2764 KB |
Output is correct |
35 |
Correct |
3 ms |
2764 KB |
Output is correct |
36 |
Correct |
3 ms |
2764 KB |
Output is correct |
37 |
Correct |
4 ms |
2764 KB |
Output is correct |
38 |
Correct |
4 ms |
2764 KB |
Output is correct |
39 |
Correct |
4 ms |
2764 KB |
Output is correct |
40 |
Correct |
3 ms |
2028 KB |
Output is correct |
41 |
Correct |
3 ms |
2764 KB |
Output is correct |
42 |
Correct |
3 ms |
2764 KB |
Output is correct |
43 |
Correct |
3 ms |
2636 KB |
Output is correct |
44 |
Correct |
3 ms |
2252 KB |
Output is correct |
45 |
Correct |
3 ms |
2636 KB |
Output is correct |
46 |
Correct |
3 ms |
2636 KB |
Output is correct |
47 |
Correct |
3 ms |
2636 KB |
Output is correct |
48 |
Correct |
3 ms |
2636 KB |
Output is correct |
49 |
Correct |
3 ms |
2636 KB |
Output is correct |
50 |
Correct |
221 ms |
67240 KB |
Output is correct |
51 |
Correct |
197 ms |
67152 KB |
Output is correct |
52 |
Correct |
206 ms |
67428 KB |
Output is correct |
53 |
Correct |
230 ms |
67204 KB |
Output is correct |
54 |
Correct |
204 ms |
67152 KB |
Output is correct |
55 |
Correct |
211 ms |
67260 KB |
Output is correct |
56 |
Correct |
257 ms |
67396 KB |
Output is correct |
57 |
Correct |
264 ms |
67212 KB |
Output is correct |
58 |
Correct |
248 ms |
67128 KB |
Output is correct |
59 |
Correct |
257 ms |
67012 KB |
Output is correct |
60 |
Correct |
251 ms |
66216 KB |
Output is correct |
61 |
Correct |
229 ms |
67304 KB |
Output is correct |
62 |
Correct |
224 ms |
67188 KB |
Output is correct |
63 |
Correct |
219 ms |
67072 KB |
Output is correct |
64 |
Correct |
249 ms |
66984 KB |
Output is correct |
65 |
Correct |
222 ms |
66888 KB |
Output is correct |
66 |
Correct |
232 ms |
67008 KB |
Output is correct |
67 |
Correct |
231 ms |
66500 KB |
Output is correct |
68 |
Correct |
213 ms |
66356 KB |
Output is correct |
69 |
Correct |
217 ms |
66460 KB |
Output is correct |
70 |
Correct |
246 ms |
66632 KB |
Output is correct |
71 |
Correct |
268 ms |
67084 KB |
Output is correct |
72 |
Correct |
234 ms |
67044 KB |
Output is correct |