#include <bits/stdc++.h>
using namespace std;
const int maxn = 400;
int dp[maxn * 2][maxn][maxn][2];
int main() {
int n;
cin>>n;
vector<int> vi(n);
for(auto &a:vi)
cin>>a;
for(int i = 0; i< maxn;i++)
for(int j = 0; j < maxn; j++)
for(int k = 0; k < maxn;k++)
for(int l = 0; l < 2; l++)
dp[i][j][k][l] = -1;
auto repmax = [&](int &cur,int x) {
cur = max(cur, x);
};
//tim, pos, last, has
if(vi[0] >= 1) {
dp[0][0][1][1] = 1;
}else
dp[0][0][0][0] = 0;
for(int i = 0; i < 2*n ;i++)
for(int j = 0; j < n; j++)
for(int k = 0;k <= n; k++) {
// cout<<i<<' '<<j<<' '<<k<<' '<<dp[i][j][k][0]<<' '<<dp[i][j][k][1]<<'\n';
if(dp[i][j][k][0]!=-1 ){
//send forward
if(j + 1 < n) {
repmax(dp[i + 1][j+1][k][0], dp[i][j][k][0]);
if(vi[j+1] >= 1 && k <= j + 1 && i + 1 <= 2*(j+1)) {
repmax(dp[i+1][j+1][j + 2][1], dp[i][j][k][0] + 1);
}
}
//send back
if(j - 1>= 0) {
repmax(dp[i + 1][j-1][k][0], dp[i][j][k][0]);
if(vi[j-1] >= 1 && k <= j - 1 && i + 1 <= 2*(j-1)) {
repmax(dp[i+1][j - 1][j][1], dp[i][j][k][0] + 1);
}
}
}
if(dp[i][j][k][1] != -1) {
if(j + 1 < n) {
if(vi[j+1] == -1) {
repmax(dp[i+1][j + 1][k][0], dp[i][j][k][1]);
}else
repmax(dp[i+ 1][j + 1][k][1], dp[i][j][k][1]);
}
if(j - 1 < n) {
if(vi[j - 1] == -1) {
repmax(dp[i + 1][j - 1][k][0], dp[i][j][k][1]);
}else
repmax(dp[i+1][j-1][k][1], dp[i][j][k][1]);
}
}
}
int maxval = 0;
for(int i = 0; i <= 2*n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k <= n; k++)
maxval = max({maxval, dp[i][j][k][0], dp[i][j][k][1]});
cout<<count(vi.begin(), vi.end(), 1) - maxval<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
501384 KB |
Output is correct |
2 |
Correct |
176 ms |
501328 KB |
Output is correct |
3 |
Correct |
180 ms |
501288 KB |
Output is correct |
4 |
Correct |
192 ms |
501300 KB |
Output is correct |
5 |
Correct |
191 ms |
501268 KB |
Output is correct |
6 |
Correct |
181 ms |
501200 KB |
Output is correct |
7 |
Correct |
175 ms |
501328 KB |
Output is correct |
8 |
Correct |
183 ms |
501276 KB |
Output is correct |
9 |
Correct |
175 ms |
501240 KB |
Output is correct |
10 |
Correct |
175 ms |
501244 KB |
Output is correct |
11 |
Correct |
190 ms |
501328 KB |
Output is correct |
12 |
Correct |
168 ms |
501344 KB |
Output is correct |
13 |
Correct |
171 ms |
501328 KB |
Output is correct |
14 |
Correct |
177 ms |
501332 KB |
Output is correct |
15 |
Correct |
223 ms |
501160 KB |
Output is correct |
16 |
Correct |
164 ms |
501300 KB |
Output is correct |
17 |
Correct |
180 ms |
501400 KB |
Output is correct |
18 |
Correct |
177 ms |
501380 KB |
Output is correct |
19 |
Correct |
174 ms |
501308 KB |
Output is correct |
20 |
Correct |
171 ms |
501276 KB |
Output is correct |
21 |
Correct |
171 ms |
501328 KB |
Output is correct |
22 |
Correct |
222 ms |
501372 KB |
Output is correct |
23 |
Correct |
179 ms |
501332 KB |
Output is correct |
24 |
Correct |
170 ms |
501172 KB |
Output is correct |
25 |
Correct |
165 ms |
501332 KB |
Output is correct |
26 |
Correct |
175 ms |
501332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
501384 KB |
Output is correct |
2 |
Correct |
176 ms |
501328 KB |
Output is correct |
3 |
Correct |
180 ms |
501288 KB |
Output is correct |
4 |
Correct |
192 ms |
501300 KB |
Output is correct |
5 |
Correct |
191 ms |
501268 KB |
Output is correct |
6 |
Correct |
181 ms |
501200 KB |
Output is correct |
7 |
Correct |
175 ms |
501328 KB |
Output is correct |
8 |
Correct |
183 ms |
501276 KB |
Output is correct |
9 |
Correct |
175 ms |
501240 KB |
Output is correct |
10 |
Correct |
175 ms |
501244 KB |
Output is correct |
11 |
Correct |
190 ms |
501328 KB |
Output is correct |
12 |
Correct |
168 ms |
501344 KB |
Output is correct |
13 |
Correct |
171 ms |
501328 KB |
Output is correct |
14 |
Correct |
177 ms |
501332 KB |
Output is correct |
15 |
Correct |
223 ms |
501160 KB |
Output is correct |
16 |
Correct |
164 ms |
501300 KB |
Output is correct |
17 |
Correct |
180 ms |
501400 KB |
Output is correct |
18 |
Correct |
177 ms |
501380 KB |
Output is correct |
19 |
Correct |
174 ms |
501308 KB |
Output is correct |
20 |
Correct |
171 ms |
501276 KB |
Output is correct |
21 |
Correct |
171 ms |
501328 KB |
Output is correct |
22 |
Correct |
222 ms |
501372 KB |
Output is correct |
23 |
Correct |
179 ms |
501332 KB |
Output is correct |
24 |
Correct |
170 ms |
501172 KB |
Output is correct |
25 |
Correct |
165 ms |
501332 KB |
Output is correct |
26 |
Correct |
175 ms |
501332 KB |
Output is correct |
27 |
Runtime error |
239 ms |
524288 KB |
Execution killed with signal 9 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
501384 KB |
Output is correct |
2 |
Correct |
176 ms |
501328 KB |
Output is correct |
3 |
Correct |
180 ms |
501288 KB |
Output is correct |
4 |
Correct |
192 ms |
501300 KB |
Output is correct |
5 |
Correct |
191 ms |
501268 KB |
Output is correct |
6 |
Correct |
181 ms |
501200 KB |
Output is correct |
7 |
Correct |
175 ms |
501328 KB |
Output is correct |
8 |
Correct |
183 ms |
501276 KB |
Output is correct |
9 |
Correct |
175 ms |
501240 KB |
Output is correct |
10 |
Correct |
175 ms |
501244 KB |
Output is correct |
11 |
Correct |
190 ms |
501328 KB |
Output is correct |
12 |
Correct |
168 ms |
501344 KB |
Output is correct |
13 |
Correct |
171 ms |
501328 KB |
Output is correct |
14 |
Correct |
177 ms |
501332 KB |
Output is correct |
15 |
Correct |
223 ms |
501160 KB |
Output is correct |
16 |
Correct |
164 ms |
501300 KB |
Output is correct |
17 |
Correct |
180 ms |
501400 KB |
Output is correct |
18 |
Correct |
177 ms |
501380 KB |
Output is correct |
19 |
Correct |
174 ms |
501308 KB |
Output is correct |
20 |
Correct |
171 ms |
501276 KB |
Output is correct |
21 |
Correct |
171 ms |
501328 KB |
Output is correct |
22 |
Correct |
222 ms |
501372 KB |
Output is correct |
23 |
Correct |
179 ms |
501332 KB |
Output is correct |
24 |
Correct |
170 ms |
501172 KB |
Output is correct |
25 |
Correct |
165 ms |
501332 KB |
Output is correct |
26 |
Correct |
175 ms |
501332 KB |
Output is correct |
27 |
Runtime error |
239 ms |
524288 KB |
Execution killed with signal 9 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
501384 KB |
Output is correct |
2 |
Correct |
176 ms |
501328 KB |
Output is correct |
3 |
Correct |
180 ms |
501288 KB |
Output is correct |
4 |
Correct |
192 ms |
501300 KB |
Output is correct |
5 |
Correct |
191 ms |
501268 KB |
Output is correct |
6 |
Correct |
181 ms |
501200 KB |
Output is correct |
7 |
Correct |
175 ms |
501328 KB |
Output is correct |
8 |
Correct |
183 ms |
501276 KB |
Output is correct |
9 |
Correct |
175 ms |
501240 KB |
Output is correct |
10 |
Correct |
175 ms |
501244 KB |
Output is correct |
11 |
Correct |
190 ms |
501328 KB |
Output is correct |
12 |
Correct |
168 ms |
501344 KB |
Output is correct |
13 |
Correct |
171 ms |
501328 KB |
Output is correct |
14 |
Correct |
177 ms |
501332 KB |
Output is correct |
15 |
Correct |
223 ms |
501160 KB |
Output is correct |
16 |
Correct |
164 ms |
501300 KB |
Output is correct |
17 |
Correct |
180 ms |
501400 KB |
Output is correct |
18 |
Correct |
177 ms |
501380 KB |
Output is correct |
19 |
Correct |
174 ms |
501308 KB |
Output is correct |
20 |
Correct |
171 ms |
501276 KB |
Output is correct |
21 |
Correct |
171 ms |
501328 KB |
Output is correct |
22 |
Correct |
222 ms |
501372 KB |
Output is correct |
23 |
Correct |
179 ms |
501332 KB |
Output is correct |
24 |
Correct |
170 ms |
501172 KB |
Output is correct |
25 |
Correct |
165 ms |
501332 KB |
Output is correct |
26 |
Correct |
175 ms |
501332 KB |
Output is correct |
27 |
Runtime error |
239 ms |
524288 KB |
Execution killed with signal 9 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
501384 KB |
Output is correct |
2 |
Correct |
176 ms |
501328 KB |
Output is correct |
3 |
Correct |
180 ms |
501288 KB |
Output is correct |
4 |
Correct |
192 ms |
501300 KB |
Output is correct |
5 |
Correct |
191 ms |
501268 KB |
Output is correct |
6 |
Correct |
181 ms |
501200 KB |
Output is correct |
7 |
Correct |
175 ms |
501328 KB |
Output is correct |
8 |
Correct |
183 ms |
501276 KB |
Output is correct |
9 |
Correct |
175 ms |
501240 KB |
Output is correct |
10 |
Correct |
175 ms |
501244 KB |
Output is correct |
11 |
Correct |
190 ms |
501328 KB |
Output is correct |
12 |
Correct |
168 ms |
501344 KB |
Output is correct |
13 |
Correct |
171 ms |
501328 KB |
Output is correct |
14 |
Correct |
177 ms |
501332 KB |
Output is correct |
15 |
Correct |
223 ms |
501160 KB |
Output is correct |
16 |
Correct |
164 ms |
501300 KB |
Output is correct |
17 |
Correct |
180 ms |
501400 KB |
Output is correct |
18 |
Correct |
177 ms |
501380 KB |
Output is correct |
19 |
Correct |
174 ms |
501308 KB |
Output is correct |
20 |
Correct |
171 ms |
501276 KB |
Output is correct |
21 |
Correct |
171 ms |
501328 KB |
Output is correct |
22 |
Correct |
222 ms |
501372 KB |
Output is correct |
23 |
Correct |
179 ms |
501332 KB |
Output is correct |
24 |
Correct |
170 ms |
501172 KB |
Output is correct |
25 |
Correct |
165 ms |
501332 KB |
Output is correct |
26 |
Correct |
175 ms |
501332 KB |
Output is correct |
27 |
Runtime error |
239 ms |
524288 KB |
Execution killed with signal 9 |
28 |
Halted |
0 ms |
0 KB |
- |