#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, ans = INF;
cin >> n;
int a[n], lt[n], rt[n], pref[n];
pair<int, int> dp[2*n+1][n];
for(int i = 0; i < n; i++) cin >> a[i];
lt[0] = a[0]==-1?0:INF;
for(int i = 1; i < n; i++) {
lt[i] = lt[i-1];
if(a[i] == -1) lt[i] = i;
}
rt[n-1] = a[n-1]==-1?n-1:INF;
for(int i = n-2; i >= 0; i--) {
rt[i] = rt[i+1];
if(a[i] == -1) rt[i] = i;
}
pref[0] = max(a[0], 0LL);
for(int i = 1; i < n; i++) pref[i] = pref[i-1]+max(a[i], 0LL);
for(int i = 0; i <= 2*n; i++) {
for(int j = 0; j < n; j++) {
dp[i][j].first = INF;
dp[i][j].second = 0;
}
}
for(int i = 0; i <= 2*n; i++) {
for(int j = 0; j < n; j++) {
if(a[j] <= 0) continue;
if(i < j) continue;
if(max(pref[j]-1, 0LL) < dp[i][j].first) {
dp[i][j] = {max(pref[j]-1, 0LL), 1};
}
int sm = 0;
for(int k = j; k < n; k++) {
if(k > j) sm += max(a[k], 0LL);
if(a[k] <= 0) continue;
int dist = min(abs(j-lt[j])+abs(k-lt[j]), abs(rt[j]-j)+abs(rt[j]-k));
//~ cout << "b " << i << " " << j << " " << k << " " << dist+i << endl;
if(dist+i > 2*n) continue;
if(dist+i > 2*k) continue;
if(k == j) {
if(dp[i][j].second < a[k] && dp[dist+i][k].first > max(dp[i][j].first-1, 0LL)) {
dp[dist+i][k] = {max(dp[i][j].first-1, 0LL), dp[i][j].second+1};
//~ cout << dp[dist+i][k].first << endl;
}
} else {
//~ cout << "b " << dp[dist+i][k].first<< " " << dp[i][j].first+max(sm-1, 0LL) << endl;
if(dp[dist+i][k].first > dp[i][j].first+max(sm-1, 0LL)) {
dp[dist+i][k] = {dp[i][j].first+max(sm-1, 0LL), 1};
//~ cout << dp[dist+i][k].first << endl;
}
}
//~ cout << "a " << i << " " << j << " " << k << " " << dist+i << " " << dp[dist+i][k].first << " " << dp[i][j].first << " " << sm << endl;
}
ans = min(ans, dp[i][j].first+pref[n-1]-pref[j]);
}
}
cout << ans;
//~ cout << endl;
//~ for(int i = 0; i <= 2*n; i++) {
//~ for(int j = 0; j < n; j++) {
//~ cout << dp[i][j].first << " ";
//~ }
//~ cout << endl;
//~ }
//~ cout << endl;
//~ for(int i = 0; i < n; i++) cout << lt[i] << " ";
//~ cout << endl;
//~ for(int i = 0; i < n; i++) cout << rt[i] << " ";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
316 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
316 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
18 ms |
2260 KB |
Output is correct |
28 |
Correct |
53 ms |
2980 KB |
Output is correct |
29 |
Correct |
35 ms |
2620 KB |
Output is correct |
30 |
Correct |
22 ms |
2260 KB |
Output is correct |
31 |
Correct |
24 ms |
3028 KB |
Output is correct |
32 |
Correct |
13 ms |
1772 KB |
Output is correct |
33 |
Correct |
26 ms |
2656 KB |
Output is correct |
34 |
Correct |
25 ms |
2260 KB |
Output is correct |
35 |
Correct |
23 ms |
3028 KB |
Output is correct |
36 |
Correct |
20 ms |
3028 KB |
Output is correct |
37 |
Correct |
24 ms |
3136 KB |
Output is correct |
38 |
Correct |
27 ms |
3256 KB |
Output is correct |
39 |
Correct |
25 ms |
3028 KB |
Output is correct |
40 |
Correct |
21 ms |
3028 KB |
Output is correct |
41 |
Correct |
19 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
316 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
18 ms |
2260 KB |
Output is correct |
28 |
Correct |
53 ms |
2980 KB |
Output is correct |
29 |
Correct |
35 ms |
2620 KB |
Output is correct |
30 |
Correct |
22 ms |
2260 KB |
Output is correct |
31 |
Correct |
24 ms |
3028 KB |
Output is correct |
32 |
Correct |
13 ms |
1772 KB |
Output is correct |
33 |
Correct |
26 ms |
2656 KB |
Output is correct |
34 |
Correct |
25 ms |
2260 KB |
Output is correct |
35 |
Correct |
23 ms |
3028 KB |
Output is correct |
36 |
Correct |
20 ms |
3028 KB |
Output is correct |
37 |
Correct |
24 ms |
3136 KB |
Output is correct |
38 |
Correct |
27 ms |
3256 KB |
Output is correct |
39 |
Correct |
25 ms |
3028 KB |
Output is correct |
40 |
Correct |
21 ms |
3028 KB |
Output is correct |
41 |
Correct |
19 ms |
3028 KB |
Output is correct |
42 |
Correct |
3 ms |
1492 KB |
Output is correct |
43 |
Incorrect |
6 ms |
1620 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
316 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
18 ms |
2260 KB |
Output is correct |
28 |
Correct |
53 ms |
2980 KB |
Output is correct |
29 |
Correct |
35 ms |
2620 KB |
Output is correct |
30 |
Correct |
22 ms |
2260 KB |
Output is correct |
31 |
Correct |
24 ms |
3028 KB |
Output is correct |
32 |
Correct |
13 ms |
1772 KB |
Output is correct |
33 |
Correct |
26 ms |
2656 KB |
Output is correct |
34 |
Correct |
25 ms |
2260 KB |
Output is correct |
35 |
Correct |
23 ms |
3028 KB |
Output is correct |
36 |
Correct |
20 ms |
3028 KB |
Output is correct |
37 |
Correct |
24 ms |
3136 KB |
Output is correct |
38 |
Correct |
27 ms |
3256 KB |
Output is correct |
39 |
Correct |
25 ms |
3028 KB |
Output is correct |
40 |
Correct |
21 ms |
3028 KB |
Output is correct |
41 |
Correct |
19 ms |
3028 KB |
Output is correct |
42 |
Correct |
3 ms |
1492 KB |
Output is correct |
43 |
Incorrect |
6 ms |
1620 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
316 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
18 ms |
2260 KB |
Output is correct |
28 |
Correct |
53 ms |
2980 KB |
Output is correct |
29 |
Correct |
35 ms |
2620 KB |
Output is correct |
30 |
Correct |
22 ms |
2260 KB |
Output is correct |
31 |
Correct |
24 ms |
3028 KB |
Output is correct |
32 |
Correct |
13 ms |
1772 KB |
Output is correct |
33 |
Correct |
26 ms |
2656 KB |
Output is correct |
34 |
Correct |
25 ms |
2260 KB |
Output is correct |
35 |
Correct |
23 ms |
3028 KB |
Output is correct |
36 |
Correct |
20 ms |
3028 KB |
Output is correct |
37 |
Correct |
24 ms |
3136 KB |
Output is correct |
38 |
Correct |
27 ms |
3256 KB |
Output is correct |
39 |
Correct |
25 ms |
3028 KB |
Output is correct |
40 |
Correct |
21 ms |
3028 KB |
Output is correct |
41 |
Correct |
19 ms |
3028 KB |
Output is correct |
42 |
Correct |
3 ms |
1492 KB |
Output is correct |
43 |
Incorrect |
6 ms |
1620 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |