#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
//~ #define int int64_t
const int N = 5005;
const int inf = 1e9;
int dp[N][N], old[N][N], a[N];
int l[N], r[N], back[N][N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=0;i<n;i++){
l[i] = n;
cin>>a[i];
if(a[i] == -1) l[i] = 0;
else if(i) l[i] = l[i-1] + 1;
}
for(int i=n-1;~i;i--){
r[i] = n;
if(a[i] == -1) r[i] = 0;
else if(i + 1 < n) r[i] = r[i+1] + 1;
}
memset(dp, 63, sizeof dp);
memset(back, 63, sizeof back);
dp[0][0] = 0;
int res = 0;
for(int i=0;i<=n;i++){
multiset<int> ss;
for(int x=0;x<=n;x++){
if(i + r[i] < N) dp[i][x] = min(dp[i][x], back[i + r[i]][x] + r[i]);
if(a[i] > 0){
old[i][x] = dp[i][x] - x * 2 * min(l[i], r[i]);
if(x){
int tmp = inf;
if(*ss.begin() + (x - 1) * 2 * min(l[i], r[i]) <= 2 * i){
dp[i][x] = min(dp[i][x], *ss.begin() + x * 2 * min(l[i], r[i]));
tmp = *ss.begin() + (x - 1) * 2 * min(l[i], r[i]);
}
if(tmp < inf){
res = max(res, x);
}
if(i + r[i] < N){
back[i + r[i]][x] = min(back[i + r[i]][x], tmp + r[i]);
}
}
ss.insert(old[i][x]);
if(x >= a[i]){
ss.erase(ss.find(old[i][x - a[i]]));
}
}
dp[i+1][x] = min(dp[i+1][x], dp[i][x] + 1);
if(dp[i][x] < inf){
res = max(res, x);
}
}
}
int tot = 0;
for(int i=0;i<n;i++){
if(~a[i]) tot += a[i];
}
cout<<tot - res<<"\n";
}
/*
7
4 2 0 2 5 2 -1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
196324 KB |
Output is correct |
2 |
Correct |
73 ms |
196332 KB |
Output is correct |
3 |
Correct |
74 ms |
196388 KB |
Output is correct |
4 |
Correct |
71 ms |
196436 KB |
Output is correct |
5 |
Correct |
71 ms |
196396 KB |
Output is correct |
6 |
Correct |
71 ms |
196380 KB |
Output is correct |
7 |
Correct |
73 ms |
196340 KB |
Output is correct |
8 |
Correct |
72 ms |
196364 KB |
Output is correct |
9 |
Correct |
72 ms |
196456 KB |
Output is correct |
10 |
Correct |
70 ms |
196344 KB |
Output is correct |
11 |
Correct |
79 ms |
196368 KB |
Output is correct |
12 |
Correct |
71 ms |
196380 KB |
Output is correct |
13 |
Correct |
71 ms |
196300 KB |
Output is correct |
14 |
Correct |
76 ms |
196308 KB |
Output is correct |
15 |
Correct |
71 ms |
196344 KB |
Output is correct |
16 |
Correct |
70 ms |
196380 KB |
Output is correct |
17 |
Correct |
71 ms |
196396 KB |
Output is correct |
18 |
Correct |
71 ms |
196332 KB |
Output is correct |
19 |
Correct |
73 ms |
196428 KB |
Output is correct |
20 |
Correct |
74 ms |
196428 KB |
Output is correct |
21 |
Correct |
73 ms |
196336 KB |
Output is correct |
22 |
Correct |
71 ms |
196296 KB |
Output is correct |
23 |
Correct |
70 ms |
196380 KB |
Output is correct |
24 |
Correct |
70 ms |
196424 KB |
Output is correct |
25 |
Correct |
70 ms |
196356 KB |
Output is correct |
26 |
Correct |
69 ms |
196376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
196324 KB |
Output is correct |
2 |
Correct |
73 ms |
196332 KB |
Output is correct |
3 |
Correct |
74 ms |
196388 KB |
Output is correct |
4 |
Correct |
71 ms |
196436 KB |
Output is correct |
5 |
Correct |
71 ms |
196396 KB |
Output is correct |
6 |
Correct |
71 ms |
196380 KB |
Output is correct |
7 |
Correct |
73 ms |
196340 KB |
Output is correct |
8 |
Correct |
72 ms |
196364 KB |
Output is correct |
9 |
Correct |
72 ms |
196456 KB |
Output is correct |
10 |
Correct |
70 ms |
196344 KB |
Output is correct |
11 |
Correct |
79 ms |
196368 KB |
Output is correct |
12 |
Correct |
71 ms |
196380 KB |
Output is correct |
13 |
Correct |
71 ms |
196300 KB |
Output is correct |
14 |
Correct |
76 ms |
196308 KB |
Output is correct |
15 |
Correct |
71 ms |
196344 KB |
Output is correct |
16 |
Correct |
70 ms |
196380 KB |
Output is correct |
17 |
Correct |
71 ms |
196396 KB |
Output is correct |
18 |
Correct |
71 ms |
196332 KB |
Output is correct |
19 |
Correct |
73 ms |
196428 KB |
Output is correct |
20 |
Correct |
74 ms |
196428 KB |
Output is correct |
21 |
Correct |
73 ms |
196336 KB |
Output is correct |
22 |
Correct |
71 ms |
196296 KB |
Output is correct |
23 |
Correct |
70 ms |
196380 KB |
Output is correct |
24 |
Correct |
70 ms |
196424 KB |
Output is correct |
25 |
Correct |
70 ms |
196356 KB |
Output is correct |
26 |
Correct |
69 ms |
196376 KB |
Output is correct |
27 |
Correct |
80 ms |
196884 KB |
Output is correct |
28 |
Correct |
74 ms |
197148 KB |
Output is correct |
29 |
Correct |
71 ms |
196952 KB |
Output is correct |
30 |
Correct |
73 ms |
196836 KB |
Output is correct |
31 |
Correct |
76 ms |
196820 KB |
Output is correct |
32 |
Correct |
72 ms |
196752 KB |
Output is correct |
33 |
Correct |
72 ms |
196984 KB |
Output is correct |
34 |
Correct |
72 ms |
196940 KB |
Output is correct |
35 |
Correct |
71 ms |
196808 KB |
Output is correct |
36 |
Correct |
73 ms |
196736 KB |
Output is correct |
37 |
Correct |
74 ms |
196920 KB |
Output is correct |
38 |
Correct |
73 ms |
196872 KB |
Output is correct |
39 |
Correct |
74 ms |
196944 KB |
Output is correct |
40 |
Correct |
72 ms |
196812 KB |
Output is correct |
41 |
Correct |
72 ms |
196748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
196324 KB |
Output is correct |
2 |
Correct |
73 ms |
196332 KB |
Output is correct |
3 |
Correct |
74 ms |
196388 KB |
Output is correct |
4 |
Correct |
71 ms |
196436 KB |
Output is correct |
5 |
Correct |
71 ms |
196396 KB |
Output is correct |
6 |
Correct |
71 ms |
196380 KB |
Output is correct |
7 |
Correct |
73 ms |
196340 KB |
Output is correct |
8 |
Correct |
72 ms |
196364 KB |
Output is correct |
9 |
Correct |
72 ms |
196456 KB |
Output is correct |
10 |
Correct |
70 ms |
196344 KB |
Output is correct |
11 |
Correct |
79 ms |
196368 KB |
Output is correct |
12 |
Correct |
71 ms |
196380 KB |
Output is correct |
13 |
Correct |
71 ms |
196300 KB |
Output is correct |
14 |
Correct |
76 ms |
196308 KB |
Output is correct |
15 |
Correct |
71 ms |
196344 KB |
Output is correct |
16 |
Correct |
70 ms |
196380 KB |
Output is correct |
17 |
Correct |
71 ms |
196396 KB |
Output is correct |
18 |
Correct |
71 ms |
196332 KB |
Output is correct |
19 |
Correct |
73 ms |
196428 KB |
Output is correct |
20 |
Correct |
74 ms |
196428 KB |
Output is correct |
21 |
Correct |
73 ms |
196336 KB |
Output is correct |
22 |
Correct |
71 ms |
196296 KB |
Output is correct |
23 |
Correct |
70 ms |
196380 KB |
Output is correct |
24 |
Correct |
70 ms |
196424 KB |
Output is correct |
25 |
Correct |
70 ms |
196356 KB |
Output is correct |
26 |
Correct |
69 ms |
196376 KB |
Output is correct |
27 |
Correct |
80 ms |
196884 KB |
Output is correct |
28 |
Correct |
74 ms |
197148 KB |
Output is correct |
29 |
Correct |
71 ms |
196952 KB |
Output is correct |
30 |
Correct |
73 ms |
196836 KB |
Output is correct |
31 |
Correct |
76 ms |
196820 KB |
Output is correct |
32 |
Correct |
72 ms |
196752 KB |
Output is correct |
33 |
Correct |
72 ms |
196984 KB |
Output is correct |
34 |
Correct |
72 ms |
196940 KB |
Output is correct |
35 |
Correct |
71 ms |
196808 KB |
Output is correct |
36 |
Correct |
73 ms |
196736 KB |
Output is correct |
37 |
Correct |
74 ms |
196920 KB |
Output is correct |
38 |
Correct |
73 ms |
196872 KB |
Output is correct |
39 |
Correct |
74 ms |
196944 KB |
Output is correct |
40 |
Correct |
72 ms |
196812 KB |
Output is correct |
41 |
Correct |
72 ms |
196748 KB |
Output is correct |
42 |
Correct |
73 ms |
196472 KB |
Output is correct |
43 |
Correct |
73 ms |
196608 KB |
Output is correct |
44 |
Correct |
72 ms |
196700 KB |
Output is correct |
45 |
Correct |
74 ms |
196744 KB |
Output is correct |
46 |
Correct |
71 ms |
196552 KB |
Output is correct |
47 |
Correct |
72 ms |
196872 KB |
Output is correct |
48 |
Correct |
77 ms |
196936 KB |
Output is correct |
49 |
Correct |
73 ms |
196788 KB |
Output is correct |
50 |
Correct |
78 ms |
197476 KB |
Output is correct |
51 |
Correct |
84 ms |
197568 KB |
Output is correct |
52 |
Correct |
76 ms |
197172 KB |
Output is correct |
53 |
Correct |
76 ms |
197804 KB |
Output is correct |
54 |
Correct |
91 ms |
197600 KB |
Output is correct |
55 |
Correct |
77 ms |
197636 KB |
Output is correct |
56 |
Correct |
77 ms |
196528 KB |
Output is correct |
57 |
Correct |
72 ms |
197036 KB |
Output is correct |
58 |
Correct |
72 ms |
197196 KB |
Output is correct |
59 |
Correct |
74 ms |
197072 KB |
Output is correct |
60 |
Correct |
75 ms |
197528 KB |
Output is correct |
61 |
Correct |
79 ms |
197028 KB |
Output is correct |
62 |
Correct |
74 ms |
197248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
196324 KB |
Output is correct |
2 |
Correct |
73 ms |
196332 KB |
Output is correct |
3 |
Correct |
74 ms |
196388 KB |
Output is correct |
4 |
Correct |
71 ms |
196436 KB |
Output is correct |
5 |
Correct |
71 ms |
196396 KB |
Output is correct |
6 |
Correct |
71 ms |
196380 KB |
Output is correct |
7 |
Correct |
73 ms |
196340 KB |
Output is correct |
8 |
Correct |
72 ms |
196364 KB |
Output is correct |
9 |
Correct |
72 ms |
196456 KB |
Output is correct |
10 |
Correct |
70 ms |
196344 KB |
Output is correct |
11 |
Correct |
79 ms |
196368 KB |
Output is correct |
12 |
Correct |
71 ms |
196380 KB |
Output is correct |
13 |
Correct |
71 ms |
196300 KB |
Output is correct |
14 |
Correct |
76 ms |
196308 KB |
Output is correct |
15 |
Correct |
71 ms |
196344 KB |
Output is correct |
16 |
Correct |
70 ms |
196380 KB |
Output is correct |
17 |
Correct |
71 ms |
196396 KB |
Output is correct |
18 |
Correct |
71 ms |
196332 KB |
Output is correct |
19 |
Correct |
73 ms |
196428 KB |
Output is correct |
20 |
Correct |
74 ms |
196428 KB |
Output is correct |
21 |
Correct |
73 ms |
196336 KB |
Output is correct |
22 |
Correct |
71 ms |
196296 KB |
Output is correct |
23 |
Correct |
70 ms |
196380 KB |
Output is correct |
24 |
Correct |
70 ms |
196424 KB |
Output is correct |
25 |
Correct |
70 ms |
196356 KB |
Output is correct |
26 |
Correct |
69 ms |
196376 KB |
Output is correct |
27 |
Correct |
80 ms |
196884 KB |
Output is correct |
28 |
Correct |
74 ms |
197148 KB |
Output is correct |
29 |
Correct |
71 ms |
196952 KB |
Output is correct |
30 |
Correct |
73 ms |
196836 KB |
Output is correct |
31 |
Correct |
76 ms |
196820 KB |
Output is correct |
32 |
Correct |
72 ms |
196752 KB |
Output is correct |
33 |
Correct |
72 ms |
196984 KB |
Output is correct |
34 |
Correct |
72 ms |
196940 KB |
Output is correct |
35 |
Correct |
71 ms |
196808 KB |
Output is correct |
36 |
Correct |
73 ms |
196736 KB |
Output is correct |
37 |
Correct |
74 ms |
196920 KB |
Output is correct |
38 |
Correct |
73 ms |
196872 KB |
Output is correct |
39 |
Correct |
74 ms |
196944 KB |
Output is correct |
40 |
Correct |
72 ms |
196812 KB |
Output is correct |
41 |
Correct |
72 ms |
196748 KB |
Output is correct |
42 |
Correct |
73 ms |
196472 KB |
Output is correct |
43 |
Correct |
73 ms |
196608 KB |
Output is correct |
44 |
Correct |
72 ms |
196700 KB |
Output is correct |
45 |
Correct |
74 ms |
196744 KB |
Output is correct |
46 |
Correct |
71 ms |
196552 KB |
Output is correct |
47 |
Correct |
72 ms |
196872 KB |
Output is correct |
48 |
Correct |
77 ms |
196936 KB |
Output is correct |
49 |
Correct |
73 ms |
196788 KB |
Output is correct |
50 |
Correct |
78 ms |
197476 KB |
Output is correct |
51 |
Correct |
84 ms |
197568 KB |
Output is correct |
52 |
Correct |
76 ms |
197172 KB |
Output is correct |
53 |
Correct |
76 ms |
197804 KB |
Output is correct |
54 |
Correct |
91 ms |
197600 KB |
Output is correct |
55 |
Correct |
77 ms |
197636 KB |
Output is correct |
56 |
Correct |
77 ms |
196528 KB |
Output is correct |
57 |
Correct |
72 ms |
197036 KB |
Output is correct |
58 |
Correct |
72 ms |
197196 KB |
Output is correct |
59 |
Correct |
74 ms |
197072 KB |
Output is correct |
60 |
Correct |
75 ms |
197528 KB |
Output is correct |
61 |
Correct |
79 ms |
197028 KB |
Output is correct |
62 |
Correct |
74 ms |
197248 KB |
Output is correct |
63 |
Correct |
408 ms |
228812 KB |
Output is correct |
64 |
Correct |
796 ms |
256628 KB |
Output is correct |
65 |
Correct |
448 ms |
228844 KB |
Output is correct |
66 |
Correct |
550 ms |
239376 KB |
Output is correct |
67 |
Correct |
565 ms |
241724 KB |
Output is correct |
68 |
Correct |
483 ms |
231988 KB |
Output is correct |
69 |
Correct |
560 ms |
239344 KB |
Output is correct |
70 |
Correct |
636 ms |
246464 KB |
Output is correct |
71 |
Correct |
2311 ms |
286528 KB |
Output is correct |
72 |
Correct |
2160 ms |
288672 KB |
Output is correct |
73 |
Correct |
2163 ms |
291576 KB |
Output is correct |
74 |
Correct |
2309 ms |
287480 KB |
Output is correct |
75 |
Correct |
162 ms |
197160 KB |
Output is correct |
76 |
Correct |
842 ms |
258160 KB |
Output is correct |
77 |
Correct |
778 ms |
259356 KB |
Output is correct |
78 |
Correct |
799 ms |
260816 KB |
Output is correct |
79 |
Correct |
787 ms |
259432 KB |
Output is correct |
80 |
Correct |
659 ms |
250240 KB |
Output is correct |
81 |
Correct |
692 ms |
252348 KB |
Output is correct |
82 |
Correct |
735 ms |
255460 KB |
Output is correct |
83 |
Correct |
900 ms |
267284 KB |
Output is correct |
84 |
Correct |
1249 ms |
252368 KB |
Output is correct |
85 |
Correct |
1331 ms |
255564 KB |
Output is correct |
86 |
Correct |
1464 ms |
261044 KB |
Output is correct |
87 |
Correct |
1647 ms |
267316 KB |
Output is correct |
88 |
Correct |
1209 ms |
250576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
196324 KB |
Output is correct |
2 |
Correct |
73 ms |
196332 KB |
Output is correct |
3 |
Correct |
74 ms |
196388 KB |
Output is correct |
4 |
Correct |
71 ms |
196436 KB |
Output is correct |
5 |
Correct |
71 ms |
196396 KB |
Output is correct |
6 |
Correct |
71 ms |
196380 KB |
Output is correct |
7 |
Correct |
73 ms |
196340 KB |
Output is correct |
8 |
Correct |
72 ms |
196364 KB |
Output is correct |
9 |
Correct |
72 ms |
196456 KB |
Output is correct |
10 |
Correct |
70 ms |
196344 KB |
Output is correct |
11 |
Correct |
79 ms |
196368 KB |
Output is correct |
12 |
Correct |
71 ms |
196380 KB |
Output is correct |
13 |
Correct |
71 ms |
196300 KB |
Output is correct |
14 |
Correct |
76 ms |
196308 KB |
Output is correct |
15 |
Correct |
71 ms |
196344 KB |
Output is correct |
16 |
Correct |
70 ms |
196380 KB |
Output is correct |
17 |
Correct |
71 ms |
196396 KB |
Output is correct |
18 |
Correct |
71 ms |
196332 KB |
Output is correct |
19 |
Correct |
73 ms |
196428 KB |
Output is correct |
20 |
Correct |
74 ms |
196428 KB |
Output is correct |
21 |
Correct |
73 ms |
196336 KB |
Output is correct |
22 |
Correct |
71 ms |
196296 KB |
Output is correct |
23 |
Correct |
70 ms |
196380 KB |
Output is correct |
24 |
Correct |
70 ms |
196424 KB |
Output is correct |
25 |
Correct |
70 ms |
196356 KB |
Output is correct |
26 |
Correct |
69 ms |
196376 KB |
Output is correct |
27 |
Correct |
80 ms |
196884 KB |
Output is correct |
28 |
Correct |
74 ms |
197148 KB |
Output is correct |
29 |
Correct |
71 ms |
196952 KB |
Output is correct |
30 |
Correct |
73 ms |
196836 KB |
Output is correct |
31 |
Correct |
76 ms |
196820 KB |
Output is correct |
32 |
Correct |
72 ms |
196752 KB |
Output is correct |
33 |
Correct |
72 ms |
196984 KB |
Output is correct |
34 |
Correct |
72 ms |
196940 KB |
Output is correct |
35 |
Correct |
71 ms |
196808 KB |
Output is correct |
36 |
Correct |
73 ms |
196736 KB |
Output is correct |
37 |
Correct |
74 ms |
196920 KB |
Output is correct |
38 |
Correct |
73 ms |
196872 KB |
Output is correct |
39 |
Correct |
74 ms |
196944 KB |
Output is correct |
40 |
Correct |
72 ms |
196812 KB |
Output is correct |
41 |
Correct |
72 ms |
196748 KB |
Output is correct |
42 |
Correct |
73 ms |
196472 KB |
Output is correct |
43 |
Correct |
73 ms |
196608 KB |
Output is correct |
44 |
Correct |
72 ms |
196700 KB |
Output is correct |
45 |
Correct |
74 ms |
196744 KB |
Output is correct |
46 |
Correct |
71 ms |
196552 KB |
Output is correct |
47 |
Correct |
72 ms |
196872 KB |
Output is correct |
48 |
Correct |
77 ms |
196936 KB |
Output is correct |
49 |
Correct |
73 ms |
196788 KB |
Output is correct |
50 |
Correct |
78 ms |
197476 KB |
Output is correct |
51 |
Correct |
84 ms |
197568 KB |
Output is correct |
52 |
Correct |
76 ms |
197172 KB |
Output is correct |
53 |
Correct |
76 ms |
197804 KB |
Output is correct |
54 |
Correct |
91 ms |
197600 KB |
Output is correct |
55 |
Correct |
77 ms |
197636 KB |
Output is correct |
56 |
Correct |
77 ms |
196528 KB |
Output is correct |
57 |
Correct |
72 ms |
197036 KB |
Output is correct |
58 |
Correct |
72 ms |
197196 KB |
Output is correct |
59 |
Correct |
74 ms |
197072 KB |
Output is correct |
60 |
Correct |
75 ms |
197528 KB |
Output is correct |
61 |
Correct |
79 ms |
197028 KB |
Output is correct |
62 |
Correct |
74 ms |
197248 KB |
Output is correct |
63 |
Correct |
408 ms |
228812 KB |
Output is correct |
64 |
Correct |
796 ms |
256628 KB |
Output is correct |
65 |
Correct |
448 ms |
228844 KB |
Output is correct |
66 |
Correct |
550 ms |
239376 KB |
Output is correct |
67 |
Correct |
565 ms |
241724 KB |
Output is correct |
68 |
Correct |
483 ms |
231988 KB |
Output is correct |
69 |
Correct |
560 ms |
239344 KB |
Output is correct |
70 |
Correct |
636 ms |
246464 KB |
Output is correct |
71 |
Correct |
2311 ms |
286528 KB |
Output is correct |
72 |
Correct |
2160 ms |
288672 KB |
Output is correct |
73 |
Correct |
2163 ms |
291576 KB |
Output is correct |
74 |
Correct |
2309 ms |
287480 KB |
Output is correct |
75 |
Correct |
162 ms |
197160 KB |
Output is correct |
76 |
Correct |
842 ms |
258160 KB |
Output is correct |
77 |
Correct |
778 ms |
259356 KB |
Output is correct |
78 |
Correct |
799 ms |
260816 KB |
Output is correct |
79 |
Correct |
787 ms |
259432 KB |
Output is correct |
80 |
Correct |
659 ms |
250240 KB |
Output is correct |
81 |
Correct |
692 ms |
252348 KB |
Output is correct |
82 |
Correct |
735 ms |
255460 KB |
Output is correct |
83 |
Correct |
900 ms |
267284 KB |
Output is correct |
84 |
Correct |
1249 ms |
252368 KB |
Output is correct |
85 |
Correct |
1331 ms |
255564 KB |
Output is correct |
86 |
Correct |
1464 ms |
261044 KB |
Output is correct |
87 |
Correct |
1647 ms |
267316 KB |
Output is correct |
88 |
Correct |
1209 ms |
250576 KB |
Output is correct |
89 |
Execution timed out |
3062 ms |
222796 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |