#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void chmax(i64 &a, i64 b) {
if (a < b)
a = b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n + 1), b, c;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == -1) {
c.push_back(i);
}
}
vector<int> l(n + 1), r(n + 1);
int lst = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == -1)
lst = i;
l[i] = lst;
}
lst = n + 1;
for (int i = n; i >= 1; i--) {
if (a[i] == -1)
lst = i;
r[i] = lst;
}
for (int i = 1; i <= n; i++) {
if (a[i] > 0) {
int x = n;
if (l[i] != 0) {
x = i - l[i];
}
if (r[i] != n + 1) {
x = min(x, r[i] - i);
}
for (int j = 1; j <= min(a[i], (2 * n + x) / (2 * x)); j++) {
b.push_back(i);
}
}
}
vector dp(2 * n + 1, vector(n + 1, vector<i64>(n + 1, -1)));
dp[0][1][0] = 0;
for (int i = 0; i < 2 * n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k < int(b.size()); k++) {
if (dp[i][j][k] == -1)
continue;
chmax(dp[i][j][k + 1], dp[i][j][k]);
int x = b[k];
if (l[x] != 0) {
int y = l[x];
if (2 + i + abs(x - j) <= 2 * x) {
chmax(dp[min(2 * n, i + abs(x - j) + 2 * abs(x - y))][x][k + 1], dp[i][j][k] + 1);
}
}
if (r[x] != n + 1) {
int y = r[x];
if (2 + i + abs(x - j) <= 2 * x) {
chmax(dp[min(2 * n, i + abs(x - j) + abs(x - y))][y][k + 1], dp[i][j][k] + 1);
}
}
}
}
}
i64 ans = 0;
for (int i = 0; i <= 2 * n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= int(b.size()); k++) {
ans = max(ans, dp[i][j][k]);
}
}
}
i64 sum = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > 0) {
sum += a[i];
}
}
cout << sum - ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
169 ms |
249804 KB |
Output is correct |
28 |
Correct |
324 ms |
399404 KB |
Output is correct |
29 |
Correct |
243 ms |
345200 KB |
Output is correct |
30 |
Correct |
182 ms |
268068 KB |
Output is correct |
31 |
Correct |
277 ms |
407792 KB |
Output is correct |
32 |
Correct |
154 ms |
161696 KB |
Output is correct |
33 |
Correct |
230 ms |
330644 KB |
Output is correct |
34 |
Correct |
196 ms |
268104 KB |
Output is correct |
35 |
Correct |
319 ms |
432816 KB |
Output is correct |
36 |
Correct |
294 ms |
432884 KB |
Output is correct |
37 |
Correct |
288 ms |
432824 KB |
Output is correct |
38 |
Correct |
288 ms |
432900 KB |
Output is correct |
39 |
Correct |
321 ms |
432936 KB |
Output is correct |
40 |
Correct |
332 ms |
432944 KB |
Output is correct |
41 |
Correct |
307 ms |
432944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
169 ms |
249804 KB |
Output is correct |
28 |
Correct |
324 ms |
399404 KB |
Output is correct |
29 |
Correct |
243 ms |
345200 KB |
Output is correct |
30 |
Correct |
182 ms |
268068 KB |
Output is correct |
31 |
Correct |
277 ms |
407792 KB |
Output is correct |
32 |
Correct |
154 ms |
161696 KB |
Output is correct |
33 |
Correct |
230 ms |
330644 KB |
Output is correct |
34 |
Correct |
196 ms |
268104 KB |
Output is correct |
35 |
Correct |
319 ms |
432816 KB |
Output is correct |
36 |
Correct |
294 ms |
432884 KB |
Output is correct |
37 |
Correct |
288 ms |
432824 KB |
Output is correct |
38 |
Correct |
288 ms |
432900 KB |
Output is correct |
39 |
Correct |
321 ms |
432936 KB |
Output is correct |
40 |
Correct |
332 ms |
432944 KB |
Output is correct |
41 |
Correct |
307 ms |
432944 KB |
Output is correct |
42 |
Correct |
132 ms |
136480 KB |
Output is correct |
43 |
Correct |
129 ms |
145908 KB |
Output is correct |
44 |
Correct |
172 ms |
217636 KB |
Output is correct |
45 |
Correct |
291 ms |
360176 KB |
Output is correct |
46 |
Correct |
126 ms |
189940 KB |
Output is correct |
47 |
Correct |
108 ms |
133788 KB |
Output is correct |
48 |
Correct |
219 ms |
280732 KB |
Output is correct |
49 |
Correct |
154 ms |
194952 KB |
Output is correct |
50 |
Execution timed out |
3071 ms |
328176 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
169 ms |
249804 KB |
Output is correct |
28 |
Correct |
324 ms |
399404 KB |
Output is correct |
29 |
Correct |
243 ms |
345200 KB |
Output is correct |
30 |
Correct |
182 ms |
268068 KB |
Output is correct |
31 |
Correct |
277 ms |
407792 KB |
Output is correct |
32 |
Correct |
154 ms |
161696 KB |
Output is correct |
33 |
Correct |
230 ms |
330644 KB |
Output is correct |
34 |
Correct |
196 ms |
268104 KB |
Output is correct |
35 |
Correct |
319 ms |
432816 KB |
Output is correct |
36 |
Correct |
294 ms |
432884 KB |
Output is correct |
37 |
Correct |
288 ms |
432824 KB |
Output is correct |
38 |
Correct |
288 ms |
432900 KB |
Output is correct |
39 |
Correct |
321 ms |
432936 KB |
Output is correct |
40 |
Correct |
332 ms |
432944 KB |
Output is correct |
41 |
Correct |
307 ms |
432944 KB |
Output is correct |
42 |
Correct |
132 ms |
136480 KB |
Output is correct |
43 |
Correct |
129 ms |
145908 KB |
Output is correct |
44 |
Correct |
172 ms |
217636 KB |
Output is correct |
45 |
Correct |
291 ms |
360176 KB |
Output is correct |
46 |
Correct |
126 ms |
189940 KB |
Output is correct |
47 |
Correct |
108 ms |
133788 KB |
Output is correct |
48 |
Correct |
219 ms |
280732 KB |
Output is correct |
49 |
Correct |
154 ms |
194952 KB |
Output is correct |
50 |
Execution timed out |
3071 ms |
328176 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
169 ms |
249804 KB |
Output is correct |
28 |
Correct |
324 ms |
399404 KB |
Output is correct |
29 |
Correct |
243 ms |
345200 KB |
Output is correct |
30 |
Correct |
182 ms |
268068 KB |
Output is correct |
31 |
Correct |
277 ms |
407792 KB |
Output is correct |
32 |
Correct |
154 ms |
161696 KB |
Output is correct |
33 |
Correct |
230 ms |
330644 KB |
Output is correct |
34 |
Correct |
196 ms |
268104 KB |
Output is correct |
35 |
Correct |
319 ms |
432816 KB |
Output is correct |
36 |
Correct |
294 ms |
432884 KB |
Output is correct |
37 |
Correct |
288 ms |
432824 KB |
Output is correct |
38 |
Correct |
288 ms |
432900 KB |
Output is correct |
39 |
Correct |
321 ms |
432936 KB |
Output is correct |
40 |
Correct |
332 ms |
432944 KB |
Output is correct |
41 |
Correct |
307 ms |
432944 KB |
Output is correct |
42 |
Correct |
132 ms |
136480 KB |
Output is correct |
43 |
Correct |
129 ms |
145908 KB |
Output is correct |
44 |
Correct |
172 ms |
217636 KB |
Output is correct |
45 |
Correct |
291 ms |
360176 KB |
Output is correct |
46 |
Correct |
126 ms |
189940 KB |
Output is correct |
47 |
Correct |
108 ms |
133788 KB |
Output is correct |
48 |
Correct |
219 ms |
280732 KB |
Output is correct |
49 |
Correct |
154 ms |
194952 KB |
Output is correct |
50 |
Execution timed out |
3071 ms |
328176 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |