#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define pb push_back
#define INF 1023456789
#define maxn 5005
int n,a[maxn],pv[maxn],nx[maxn];
vector<int> shop,play;
int memo[2*maxn][maxn][2];
int dp(int t,int i,int hv){
if(i>=shop.size()||t>2*n)return 0;
if(memo[t][i][hv]!=-1)return memo[t][i][hv];
int sd=INF;
if(pv[i]!=-1)sd=min(sd,shop[i]-play[pv[i]]);
if(nx[i]<play.size())sd=min(sd,play[nx[i]]-shop[i]);
int ans=0;
//transition 0: don't do anything
if(hv==0)ans=max(ans,dp(t+shop[i+1]-shop[i],i+1,0));
else{
ans=max(ans,dp(t+shop[i+1]-shop[i]+2*sd,i+1,0));
if(nx[i]<play.size()&&play[nx[i]]<shop[i+1]){
ans=max(ans,dp(t+shop[i+1]-shop[i],i+1,0));
}
ans=max(ans,dp(t+shop[i+1]-shop[i],i+1,1));
}
for(int x=1;x<=a[shop[i]];++x){
int add=0;
if(hv)add=hv*2*sd;
if(t+2*(x-1)*sd+add>2*shop[i])break;
//transition 1: back and forth x times
ans=max(ans,dp(t+2*x*sd+shop[i+1]-shop[i]+add,i+1,0)+x);
//transition 2: back and forth x-1 times, carry 1 to a playground in between i and i+1
if(nx[i]<play.size()&&play[nx[i]]<shop[i+1]){
ans=max(ans,dp(t+2*(x-1)*sd+shop[i+1]-shop[i]+add,i+1,0)+x);
}
//transition 3: back and forth x-1 times, carry 1 to i+1, then let i+1 settle that
ans=max(ans,dp(t+2*(x-1)*sd+shop[i+1]-shop[i]+add,i+1,1)+x);
}
//pf("dp %d %d %d: %d\n",t,i,hv,ans);
return memo[t][i][hv]=ans;
}
int main(){
sf("%d",&n);
int sm=0;
for(int i=0;i<n;++i){
sf("%d",&a[i]);
if(a[i]==-1)play.pb(i);
else shop.pb(i),sm+=a[i];
}
if(shop.size()==0){
pf("0\n");
return 0;
}
shop.pb(n);
int cur=-1;
for(int x=0;x<shop.size();++x){
int i=shop[x];
while(cur+1<play.size()&&play[cur+1]<i)++cur;
pv[x]=cur;
nx[x]=cur+1;
}
memset(memo,-1,sizeof memo);
pf("%d\n",sm-dp(shop[0],0,0));
}
Compilation message
tortoise.cpp: In function 'int dp(int, int, int)':
tortoise.cpp:16:6: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(i>=shop.size()||t>2*n)return 0;
| ~^~~~~~~~~~~~~
tortoise.cpp:21:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if(nx[i]<play.size())sd=min(sd,play[nx[i]]-shop[i]);
| ~~~~~^~~~~~~~~~~~
tortoise.cpp:29:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(nx[i]<play.size()&&play[nx[i]]<shop[i+1]){
| ~~~~~^~~~~~~~~~~~
tortoise.cpp:45:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if(nx[i]<play.size()&&play[nx[i]]<shop[i+1]){
| ~~~~~^~~~~~~~~~~~
tortoise.cpp: In function 'int main()':
tortoise.cpp:71:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int x=0;x<shop.size();++x){
| ~^~~~~~~~~~~~
tortoise.cpp:73:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | while(cur+1<play.size()&&play[cur+1]<i)++cur;
| ~~~~~^~~~~~~~~~~~
tortoise.cpp:58:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | sf("%d",&n);
| ^
tortoise.cpp:61:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | sf("%d",&a[i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
392436 KB |
Output is correct |
2 |
Correct |
150 ms |
392448 KB |
Output is correct |
3 |
Correct |
153 ms |
392476 KB |
Output is correct |
4 |
Correct |
162 ms |
392548 KB |
Output is correct |
5 |
Correct |
150 ms |
392472 KB |
Output is correct |
6 |
Correct |
154 ms |
392412 KB |
Output is correct |
7 |
Correct |
150 ms |
392468 KB |
Output is correct |
8 |
Correct |
158 ms |
392460 KB |
Output is correct |
9 |
Correct |
155 ms |
392416 KB |
Output is correct |
10 |
Correct |
151 ms |
392376 KB |
Output is correct |
11 |
Correct |
156 ms |
392396 KB |
Output is correct |
12 |
Correct |
155 ms |
392464 KB |
Output is correct |
13 |
Correct |
160 ms |
392412 KB |
Output is correct |
14 |
Correct |
146 ms |
392416 KB |
Output is correct |
15 |
Correct |
150 ms |
392352 KB |
Output is correct |
16 |
Correct |
144 ms |
392408 KB |
Output is correct |
17 |
Correct |
150 ms |
392420 KB |
Output is correct |
18 |
Correct |
154 ms |
392436 KB |
Output is correct |
19 |
Correct |
153 ms |
392492 KB |
Output is correct |
20 |
Correct |
145 ms |
392508 KB |
Output is correct |
21 |
Correct |
143 ms |
392364 KB |
Output is correct |
22 |
Correct |
143 ms |
392476 KB |
Output is correct |
23 |
Correct |
150 ms |
392364 KB |
Output is correct |
24 |
Correct |
147 ms |
392456 KB |
Output is correct |
25 |
Correct |
155 ms |
392396 KB |
Output is correct |
26 |
Correct |
156 ms |
392376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
392436 KB |
Output is correct |
2 |
Correct |
150 ms |
392448 KB |
Output is correct |
3 |
Correct |
153 ms |
392476 KB |
Output is correct |
4 |
Correct |
162 ms |
392548 KB |
Output is correct |
5 |
Correct |
150 ms |
392472 KB |
Output is correct |
6 |
Correct |
154 ms |
392412 KB |
Output is correct |
7 |
Correct |
150 ms |
392468 KB |
Output is correct |
8 |
Correct |
158 ms |
392460 KB |
Output is correct |
9 |
Correct |
155 ms |
392416 KB |
Output is correct |
10 |
Correct |
151 ms |
392376 KB |
Output is correct |
11 |
Correct |
156 ms |
392396 KB |
Output is correct |
12 |
Correct |
155 ms |
392464 KB |
Output is correct |
13 |
Correct |
160 ms |
392412 KB |
Output is correct |
14 |
Correct |
146 ms |
392416 KB |
Output is correct |
15 |
Correct |
150 ms |
392352 KB |
Output is correct |
16 |
Correct |
144 ms |
392408 KB |
Output is correct |
17 |
Correct |
150 ms |
392420 KB |
Output is correct |
18 |
Correct |
154 ms |
392436 KB |
Output is correct |
19 |
Correct |
153 ms |
392492 KB |
Output is correct |
20 |
Correct |
145 ms |
392508 KB |
Output is correct |
21 |
Correct |
143 ms |
392364 KB |
Output is correct |
22 |
Correct |
143 ms |
392476 KB |
Output is correct |
23 |
Correct |
150 ms |
392364 KB |
Output is correct |
24 |
Correct |
147 ms |
392456 KB |
Output is correct |
25 |
Correct |
155 ms |
392396 KB |
Output is correct |
26 |
Correct |
156 ms |
392376 KB |
Output is correct |
27 |
Correct |
152 ms |
392484 KB |
Output is correct |
28 |
Correct |
171 ms |
392388 KB |
Output is correct |
29 |
Correct |
151 ms |
392468 KB |
Output is correct |
30 |
Correct |
151 ms |
392440 KB |
Output is correct |
31 |
Correct |
156 ms |
392408 KB |
Output is correct |
32 |
Correct |
177 ms |
392492 KB |
Output is correct |
33 |
Correct |
155 ms |
392508 KB |
Output is correct |
34 |
Correct |
151 ms |
392480 KB |
Output is correct |
35 |
Correct |
150 ms |
392484 KB |
Output is correct |
36 |
Correct |
158 ms |
392496 KB |
Output is correct |
37 |
Correct |
155 ms |
392424 KB |
Output is correct |
38 |
Correct |
150 ms |
392388 KB |
Output is correct |
39 |
Correct |
148 ms |
392488 KB |
Output is correct |
40 |
Correct |
146 ms |
392468 KB |
Output is correct |
41 |
Correct |
161 ms |
392412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
392436 KB |
Output is correct |
2 |
Correct |
150 ms |
392448 KB |
Output is correct |
3 |
Correct |
153 ms |
392476 KB |
Output is correct |
4 |
Correct |
162 ms |
392548 KB |
Output is correct |
5 |
Correct |
150 ms |
392472 KB |
Output is correct |
6 |
Correct |
154 ms |
392412 KB |
Output is correct |
7 |
Correct |
150 ms |
392468 KB |
Output is correct |
8 |
Correct |
158 ms |
392460 KB |
Output is correct |
9 |
Correct |
155 ms |
392416 KB |
Output is correct |
10 |
Correct |
151 ms |
392376 KB |
Output is correct |
11 |
Correct |
156 ms |
392396 KB |
Output is correct |
12 |
Correct |
155 ms |
392464 KB |
Output is correct |
13 |
Correct |
160 ms |
392412 KB |
Output is correct |
14 |
Correct |
146 ms |
392416 KB |
Output is correct |
15 |
Correct |
150 ms |
392352 KB |
Output is correct |
16 |
Correct |
144 ms |
392408 KB |
Output is correct |
17 |
Correct |
150 ms |
392420 KB |
Output is correct |
18 |
Correct |
154 ms |
392436 KB |
Output is correct |
19 |
Correct |
153 ms |
392492 KB |
Output is correct |
20 |
Correct |
145 ms |
392508 KB |
Output is correct |
21 |
Correct |
143 ms |
392364 KB |
Output is correct |
22 |
Correct |
143 ms |
392476 KB |
Output is correct |
23 |
Correct |
150 ms |
392364 KB |
Output is correct |
24 |
Correct |
147 ms |
392456 KB |
Output is correct |
25 |
Correct |
155 ms |
392396 KB |
Output is correct |
26 |
Correct |
156 ms |
392376 KB |
Output is correct |
27 |
Correct |
152 ms |
392484 KB |
Output is correct |
28 |
Correct |
171 ms |
392388 KB |
Output is correct |
29 |
Correct |
151 ms |
392468 KB |
Output is correct |
30 |
Correct |
151 ms |
392440 KB |
Output is correct |
31 |
Correct |
156 ms |
392408 KB |
Output is correct |
32 |
Correct |
177 ms |
392492 KB |
Output is correct |
33 |
Correct |
155 ms |
392508 KB |
Output is correct |
34 |
Correct |
151 ms |
392480 KB |
Output is correct |
35 |
Correct |
150 ms |
392484 KB |
Output is correct |
36 |
Correct |
158 ms |
392496 KB |
Output is correct |
37 |
Correct |
155 ms |
392424 KB |
Output is correct |
38 |
Correct |
150 ms |
392388 KB |
Output is correct |
39 |
Correct |
148 ms |
392488 KB |
Output is correct |
40 |
Correct |
146 ms |
392468 KB |
Output is correct |
41 |
Correct |
161 ms |
392412 KB |
Output is correct |
42 |
Correct |
150 ms |
392388 KB |
Output is correct |
43 |
Correct |
206 ms |
392428 KB |
Output is correct |
44 |
Correct |
146 ms |
392392 KB |
Output is correct |
45 |
Correct |
164 ms |
392396 KB |
Output is correct |
46 |
Correct |
160 ms |
392484 KB |
Output is correct |
47 |
Correct |
168 ms |
392400 KB |
Output is correct |
48 |
Correct |
146 ms |
392564 KB |
Output is correct |
49 |
Correct |
148 ms |
392396 KB |
Output is correct |
50 |
Correct |
171 ms |
392400 KB |
Output is correct |
51 |
Correct |
150 ms |
392500 KB |
Output is correct |
52 |
Correct |
153 ms |
392396 KB |
Output is correct |
53 |
Correct |
190 ms |
392396 KB |
Output is correct |
54 |
Correct |
166 ms |
392612 KB |
Output is correct |
55 |
Correct |
150 ms |
392452 KB |
Output is correct |
56 |
Correct |
146 ms |
392484 KB |
Output is correct |
57 |
Correct |
151 ms |
392428 KB |
Output is correct |
58 |
Correct |
152 ms |
392416 KB |
Output is correct |
59 |
Correct |
150 ms |
392508 KB |
Output is correct |
60 |
Correct |
151 ms |
392504 KB |
Output is correct |
61 |
Correct |
158 ms |
392516 KB |
Output is correct |
62 |
Correct |
150 ms |
392504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
392436 KB |
Output is correct |
2 |
Correct |
150 ms |
392448 KB |
Output is correct |
3 |
Correct |
153 ms |
392476 KB |
Output is correct |
4 |
Correct |
162 ms |
392548 KB |
Output is correct |
5 |
Correct |
150 ms |
392472 KB |
Output is correct |
6 |
Correct |
154 ms |
392412 KB |
Output is correct |
7 |
Correct |
150 ms |
392468 KB |
Output is correct |
8 |
Correct |
158 ms |
392460 KB |
Output is correct |
9 |
Correct |
155 ms |
392416 KB |
Output is correct |
10 |
Correct |
151 ms |
392376 KB |
Output is correct |
11 |
Correct |
156 ms |
392396 KB |
Output is correct |
12 |
Correct |
155 ms |
392464 KB |
Output is correct |
13 |
Correct |
160 ms |
392412 KB |
Output is correct |
14 |
Correct |
146 ms |
392416 KB |
Output is correct |
15 |
Correct |
150 ms |
392352 KB |
Output is correct |
16 |
Correct |
144 ms |
392408 KB |
Output is correct |
17 |
Correct |
150 ms |
392420 KB |
Output is correct |
18 |
Correct |
154 ms |
392436 KB |
Output is correct |
19 |
Correct |
153 ms |
392492 KB |
Output is correct |
20 |
Correct |
145 ms |
392508 KB |
Output is correct |
21 |
Correct |
143 ms |
392364 KB |
Output is correct |
22 |
Correct |
143 ms |
392476 KB |
Output is correct |
23 |
Correct |
150 ms |
392364 KB |
Output is correct |
24 |
Correct |
147 ms |
392456 KB |
Output is correct |
25 |
Correct |
155 ms |
392396 KB |
Output is correct |
26 |
Correct |
156 ms |
392376 KB |
Output is correct |
27 |
Correct |
152 ms |
392484 KB |
Output is correct |
28 |
Correct |
171 ms |
392388 KB |
Output is correct |
29 |
Correct |
151 ms |
392468 KB |
Output is correct |
30 |
Correct |
151 ms |
392440 KB |
Output is correct |
31 |
Correct |
156 ms |
392408 KB |
Output is correct |
32 |
Correct |
177 ms |
392492 KB |
Output is correct |
33 |
Correct |
155 ms |
392508 KB |
Output is correct |
34 |
Correct |
151 ms |
392480 KB |
Output is correct |
35 |
Correct |
150 ms |
392484 KB |
Output is correct |
36 |
Correct |
158 ms |
392496 KB |
Output is correct |
37 |
Correct |
155 ms |
392424 KB |
Output is correct |
38 |
Correct |
150 ms |
392388 KB |
Output is correct |
39 |
Correct |
148 ms |
392488 KB |
Output is correct |
40 |
Correct |
146 ms |
392468 KB |
Output is correct |
41 |
Correct |
161 ms |
392412 KB |
Output is correct |
42 |
Correct |
150 ms |
392388 KB |
Output is correct |
43 |
Correct |
206 ms |
392428 KB |
Output is correct |
44 |
Correct |
146 ms |
392392 KB |
Output is correct |
45 |
Correct |
164 ms |
392396 KB |
Output is correct |
46 |
Correct |
160 ms |
392484 KB |
Output is correct |
47 |
Correct |
168 ms |
392400 KB |
Output is correct |
48 |
Correct |
146 ms |
392564 KB |
Output is correct |
49 |
Correct |
148 ms |
392396 KB |
Output is correct |
50 |
Correct |
171 ms |
392400 KB |
Output is correct |
51 |
Correct |
150 ms |
392500 KB |
Output is correct |
52 |
Correct |
153 ms |
392396 KB |
Output is correct |
53 |
Correct |
190 ms |
392396 KB |
Output is correct |
54 |
Correct |
166 ms |
392612 KB |
Output is correct |
55 |
Correct |
150 ms |
392452 KB |
Output is correct |
56 |
Correct |
146 ms |
392484 KB |
Output is correct |
57 |
Correct |
151 ms |
392428 KB |
Output is correct |
58 |
Correct |
152 ms |
392416 KB |
Output is correct |
59 |
Correct |
150 ms |
392508 KB |
Output is correct |
60 |
Correct |
151 ms |
392504 KB |
Output is correct |
61 |
Correct |
158 ms |
392516 KB |
Output is correct |
62 |
Correct |
150 ms |
392504 KB |
Output is correct |
63 |
Correct |
631 ms |
393044 KB |
Output is correct |
64 |
Correct |
1143 ms |
393164 KB |
Output is correct |
65 |
Correct |
734 ms |
393148 KB |
Output is correct |
66 |
Correct |
814 ms |
393060 KB |
Output is correct |
67 |
Correct |
772 ms |
393172 KB |
Output is correct |
68 |
Correct |
759 ms |
393164 KB |
Output is correct |
69 |
Correct |
924 ms |
393124 KB |
Output is correct |
70 |
Correct |
852 ms |
393124 KB |
Output is correct |
71 |
Execution timed out |
3079 ms |
393148 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
392436 KB |
Output is correct |
2 |
Correct |
150 ms |
392448 KB |
Output is correct |
3 |
Correct |
153 ms |
392476 KB |
Output is correct |
4 |
Correct |
162 ms |
392548 KB |
Output is correct |
5 |
Correct |
150 ms |
392472 KB |
Output is correct |
6 |
Correct |
154 ms |
392412 KB |
Output is correct |
7 |
Correct |
150 ms |
392468 KB |
Output is correct |
8 |
Correct |
158 ms |
392460 KB |
Output is correct |
9 |
Correct |
155 ms |
392416 KB |
Output is correct |
10 |
Correct |
151 ms |
392376 KB |
Output is correct |
11 |
Correct |
156 ms |
392396 KB |
Output is correct |
12 |
Correct |
155 ms |
392464 KB |
Output is correct |
13 |
Correct |
160 ms |
392412 KB |
Output is correct |
14 |
Correct |
146 ms |
392416 KB |
Output is correct |
15 |
Correct |
150 ms |
392352 KB |
Output is correct |
16 |
Correct |
144 ms |
392408 KB |
Output is correct |
17 |
Correct |
150 ms |
392420 KB |
Output is correct |
18 |
Correct |
154 ms |
392436 KB |
Output is correct |
19 |
Correct |
153 ms |
392492 KB |
Output is correct |
20 |
Correct |
145 ms |
392508 KB |
Output is correct |
21 |
Correct |
143 ms |
392364 KB |
Output is correct |
22 |
Correct |
143 ms |
392476 KB |
Output is correct |
23 |
Correct |
150 ms |
392364 KB |
Output is correct |
24 |
Correct |
147 ms |
392456 KB |
Output is correct |
25 |
Correct |
155 ms |
392396 KB |
Output is correct |
26 |
Correct |
156 ms |
392376 KB |
Output is correct |
27 |
Correct |
152 ms |
392484 KB |
Output is correct |
28 |
Correct |
171 ms |
392388 KB |
Output is correct |
29 |
Correct |
151 ms |
392468 KB |
Output is correct |
30 |
Correct |
151 ms |
392440 KB |
Output is correct |
31 |
Correct |
156 ms |
392408 KB |
Output is correct |
32 |
Correct |
177 ms |
392492 KB |
Output is correct |
33 |
Correct |
155 ms |
392508 KB |
Output is correct |
34 |
Correct |
151 ms |
392480 KB |
Output is correct |
35 |
Correct |
150 ms |
392484 KB |
Output is correct |
36 |
Correct |
158 ms |
392496 KB |
Output is correct |
37 |
Correct |
155 ms |
392424 KB |
Output is correct |
38 |
Correct |
150 ms |
392388 KB |
Output is correct |
39 |
Correct |
148 ms |
392488 KB |
Output is correct |
40 |
Correct |
146 ms |
392468 KB |
Output is correct |
41 |
Correct |
161 ms |
392412 KB |
Output is correct |
42 |
Correct |
150 ms |
392388 KB |
Output is correct |
43 |
Correct |
206 ms |
392428 KB |
Output is correct |
44 |
Correct |
146 ms |
392392 KB |
Output is correct |
45 |
Correct |
164 ms |
392396 KB |
Output is correct |
46 |
Correct |
160 ms |
392484 KB |
Output is correct |
47 |
Correct |
168 ms |
392400 KB |
Output is correct |
48 |
Correct |
146 ms |
392564 KB |
Output is correct |
49 |
Correct |
148 ms |
392396 KB |
Output is correct |
50 |
Correct |
171 ms |
392400 KB |
Output is correct |
51 |
Correct |
150 ms |
392500 KB |
Output is correct |
52 |
Correct |
153 ms |
392396 KB |
Output is correct |
53 |
Correct |
190 ms |
392396 KB |
Output is correct |
54 |
Correct |
166 ms |
392612 KB |
Output is correct |
55 |
Correct |
150 ms |
392452 KB |
Output is correct |
56 |
Correct |
146 ms |
392484 KB |
Output is correct |
57 |
Correct |
151 ms |
392428 KB |
Output is correct |
58 |
Correct |
152 ms |
392416 KB |
Output is correct |
59 |
Correct |
150 ms |
392508 KB |
Output is correct |
60 |
Correct |
151 ms |
392504 KB |
Output is correct |
61 |
Correct |
158 ms |
392516 KB |
Output is correct |
62 |
Correct |
150 ms |
392504 KB |
Output is correct |
63 |
Correct |
631 ms |
393044 KB |
Output is correct |
64 |
Correct |
1143 ms |
393164 KB |
Output is correct |
65 |
Correct |
734 ms |
393148 KB |
Output is correct |
66 |
Correct |
814 ms |
393060 KB |
Output is correct |
67 |
Correct |
772 ms |
393172 KB |
Output is correct |
68 |
Correct |
759 ms |
393164 KB |
Output is correct |
69 |
Correct |
924 ms |
393124 KB |
Output is correct |
70 |
Correct |
852 ms |
393124 KB |
Output is correct |
71 |
Execution timed out |
3079 ms |
393148 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |