#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 two pointer
// Observation #2 : if the size of row[j + 1, k] is higher than row[i, j]
// then for every other k >= current_k
// the value of the dp is constant
// this is also correct for the opposite
// So we can solve this in O(N^2) by precomputing the transitions for every j
// and using two pointer to increment k and decrement i
struct segtree{
int v[MX * 2];
void build(int p){
for(int i = MX; i < 2 * MX; i++) v[i] = dp[i - MX][p];
for(int i = MX - 1; i > 0; i--) v[i] = max(v[i << 1], v[i << 1 | 1]);
}
int range(int l, int r){ // [l, r]
int res = 0;
for(l += MX, r += MX + 1; l < r; l >>= 1, r >>= 1){
if(l & 1) chmax(res, v[l++]);
if(r & 1) chmax(res, v[--r]);
}
return res;
}
} st;
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;
st.build(p);
while(i >= 0 && k < N){
if(bot == top){
cnt++; chmax(dp[p + 1][k], st.range(0, 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 |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
588 KB |
Output is correct |
3 |
Correct |
4 ms |
540 KB |
Output is correct |
4 |
Correct |
4 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
4 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
588 KB |
Output is correct |
3 |
Correct |
4 ms |
540 KB |
Output is correct |
4 |
Correct |
4 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
4 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
9 ms |
844 KB |
Output is correct |
11 |
Correct |
10 ms |
840 KB |
Output is correct |
12 |
Correct |
8 ms |
744 KB |
Output is correct |
13 |
Correct |
8 ms |
844 KB |
Output is correct |
14 |
Correct |
8 ms |
848 KB |
Output is correct |
15 |
Correct |
8 ms |
844 KB |
Output is correct |
16 |
Correct |
7 ms |
588 KB |
Output is correct |
17 |
Correct |
9 ms |
796 KB |
Output is correct |
18 |
Correct |
8 ms |
844 KB |
Output is correct |
19 |
Correct |
9 ms |
716 KB |
Output is correct |
20 |
Correct |
9 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2940 KB |
Output is correct |
2 |
Correct |
37 ms |
3012 KB |
Output is correct |
3 |
Correct |
37 ms |
2988 KB |
Output is correct |
4 |
Correct |
31 ms |
3012 KB |
Output is correct |
5 |
Correct |
32 ms |
3008 KB |
Output is correct |
6 |
Correct |
32 ms |
3000 KB |
Output is correct |
7 |
Correct |
32 ms |
2932 KB |
Output is correct |
8 |
Correct |
37 ms |
2908 KB |
Output is correct |
9 |
Correct |
33 ms |
3012 KB |
Output is correct |
10 |
Correct |
31 ms |
2972 KB |
Output is correct |
11 |
Correct |
39 ms |
2988 KB |
Output is correct |
12 |
Correct |
34 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
588 KB |
Output is correct |
3 |
Correct |
4 ms |
540 KB |
Output is correct |
4 |
Correct |
4 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
4 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
9 ms |
844 KB |
Output is correct |
11 |
Correct |
10 ms |
840 KB |
Output is correct |
12 |
Correct |
8 ms |
744 KB |
Output is correct |
13 |
Correct |
8 ms |
844 KB |
Output is correct |
14 |
Correct |
8 ms |
848 KB |
Output is correct |
15 |
Correct |
8 ms |
844 KB |
Output is correct |
16 |
Correct |
7 ms |
588 KB |
Output is correct |
17 |
Correct |
9 ms |
796 KB |
Output is correct |
18 |
Correct |
8 ms |
844 KB |
Output is correct |
19 |
Correct |
9 ms |
716 KB |
Output is correct |
20 |
Correct |
9 ms |
744 KB |
Output is correct |
21 |
Correct |
31 ms |
2940 KB |
Output is correct |
22 |
Correct |
37 ms |
3012 KB |
Output is correct |
23 |
Correct |
37 ms |
2988 KB |
Output is correct |
24 |
Correct |
31 ms |
3012 KB |
Output is correct |
25 |
Correct |
32 ms |
3008 KB |
Output is correct |
26 |
Correct |
32 ms |
3000 KB |
Output is correct |
27 |
Correct |
32 ms |
2932 KB |
Output is correct |
28 |
Correct |
37 ms |
2908 KB |
Output is correct |
29 |
Correct |
33 ms |
3012 KB |
Output is correct |
30 |
Correct |
31 ms |
2972 KB |
Output is correct |
31 |
Correct |
39 ms |
2988 KB |
Output is correct |
32 |
Correct |
34 ms |
2944 KB |
Output is correct |
33 |
Correct |
31 ms |
2908 KB |
Output is correct |
34 |
Correct |
32 ms |
2972 KB |
Output is correct |
35 |
Correct |
31 ms |
2984 KB |
Output is correct |
36 |
Correct |
32 ms |
3004 KB |
Output is correct |
37 |
Correct |
32 ms |
2972 KB |
Output is correct |
38 |
Correct |
34 ms |
2884 KB |
Output is correct |
39 |
Correct |
37 ms |
2992 KB |
Output is correct |
40 |
Correct |
30 ms |
2232 KB |
Output is correct |
41 |
Correct |
32 ms |
2924 KB |
Output is correct |
42 |
Correct |
30 ms |
2980 KB |
Output is correct |
43 |
Correct |
30 ms |
2924 KB |
Output is correct |
44 |
Correct |
30 ms |
2380 KB |
Output is correct |
45 |
Correct |
30 ms |
2820 KB |
Output is correct |
46 |
Correct |
31 ms |
2792 KB |
Output is correct |
47 |
Correct |
31 ms |
2904 KB |
Output is correct |
48 |
Correct |
31 ms |
2800 KB |
Output is correct |
49 |
Correct |
31 ms |
2884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
4 ms |
588 KB |
Output is correct |
3 |
Correct |
4 ms |
540 KB |
Output is correct |
4 |
Correct |
4 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
4 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
9 ms |
844 KB |
Output is correct |
11 |
Correct |
10 ms |
840 KB |
Output is correct |
12 |
Correct |
8 ms |
744 KB |
Output is correct |
13 |
Correct |
8 ms |
844 KB |
Output is correct |
14 |
Correct |
8 ms |
848 KB |
Output is correct |
15 |
Correct |
8 ms |
844 KB |
Output is correct |
16 |
Correct |
7 ms |
588 KB |
Output is correct |
17 |
Correct |
9 ms |
796 KB |
Output is correct |
18 |
Correct |
8 ms |
844 KB |
Output is correct |
19 |
Correct |
9 ms |
716 KB |
Output is correct |
20 |
Correct |
9 ms |
744 KB |
Output is correct |
21 |
Correct |
31 ms |
2940 KB |
Output is correct |
22 |
Correct |
37 ms |
3012 KB |
Output is correct |
23 |
Correct |
37 ms |
2988 KB |
Output is correct |
24 |
Correct |
31 ms |
3012 KB |
Output is correct |
25 |
Correct |
32 ms |
3008 KB |
Output is correct |
26 |
Correct |
32 ms |
3000 KB |
Output is correct |
27 |
Correct |
32 ms |
2932 KB |
Output is correct |
28 |
Correct |
37 ms |
2908 KB |
Output is correct |
29 |
Correct |
33 ms |
3012 KB |
Output is correct |
30 |
Correct |
31 ms |
2972 KB |
Output is correct |
31 |
Correct |
39 ms |
2988 KB |
Output is correct |
32 |
Correct |
34 ms |
2944 KB |
Output is correct |
33 |
Correct |
31 ms |
2908 KB |
Output is correct |
34 |
Correct |
32 ms |
2972 KB |
Output is correct |
35 |
Correct |
31 ms |
2984 KB |
Output is correct |
36 |
Correct |
32 ms |
3004 KB |
Output is correct |
37 |
Correct |
32 ms |
2972 KB |
Output is correct |
38 |
Correct |
34 ms |
2884 KB |
Output is correct |
39 |
Correct |
37 ms |
2992 KB |
Output is correct |
40 |
Correct |
30 ms |
2232 KB |
Output is correct |
41 |
Correct |
32 ms |
2924 KB |
Output is correct |
42 |
Correct |
30 ms |
2980 KB |
Output is correct |
43 |
Correct |
30 ms |
2924 KB |
Output is correct |
44 |
Correct |
30 ms |
2380 KB |
Output is correct |
45 |
Correct |
30 ms |
2820 KB |
Output is correct |
46 |
Correct |
31 ms |
2792 KB |
Output is correct |
47 |
Correct |
31 ms |
2904 KB |
Output is correct |
48 |
Correct |
31 ms |
2800 KB |
Output is correct |
49 |
Correct |
31 ms |
2884 KB |
Output is correct |
50 |
Correct |
505 ms |
67268 KB |
Output is correct |
51 |
Correct |
502 ms |
67388 KB |
Output is correct |
52 |
Correct |
530 ms |
67208 KB |
Output is correct |
53 |
Correct |
519 ms |
67276 KB |
Output is correct |
54 |
Correct |
557 ms |
67288 KB |
Output is correct |
55 |
Correct |
564 ms |
67288 KB |
Output is correct |
56 |
Correct |
623 ms |
67264 KB |
Output is correct |
57 |
Correct |
595 ms |
67284 KB |
Output is correct |
58 |
Correct |
473 ms |
67300 KB |
Output is correct |
59 |
Correct |
418 ms |
67012 KB |
Output is correct |
60 |
Correct |
402 ms |
66348 KB |
Output is correct |
61 |
Correct |
376 ms |
67212 KB |
Output is correct |
62 |
Correct |
375 ms |
67144 KB |
Output is correct |
63 |
Correct |
363 ms |
66944 KB |
Output is correct |
64 |
Correct |
380 ms |
67136 KB |
Output is correct |
65 |
Correct |
373 ms |
66932 KB |
Output is correct |
66 |
Correct |
411 ms |
66728 KB |
Output is correct |
67 |
Correct |
367 ms |
66500 KB |
Output is correct |
68 |
Correct |
373 ms |
66436 KB |
Output is correct |
69 |
Correct |
372 ms |
66364 KB |
Output is correct |
70 |
Correct |
432 ms |
66468 KB |
Output is correct |
71 |
Correct |
418 ms |
67072 KB |
Output is correct |
72 |
Correct |
387 ms |
67088 KB |
Output is correct |