#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define pb push_back
#define INF 1023456789
#define maxn 305
int n,a[maxn],pv[maxn],nx[maxn];
vector<int> shop,play;
int memo[2*maxn][maxn];
int dp(int t,int i){
if(i>shop.size()||t>2*n)return 0;
if(memo[t][i]!=-1)return memo[t][i];
int num=0;
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 cur=t,ans=0;
for(int x=0;x<=a[shop[i]];++x){
//back and forth x-1 times, then get 1 and go to another shop
if(cur>2*shop[i])break;
for(int y=i;y<shop.size();++y){
int take=INF;
if(x==0)take=min(take,abs(shop[y]-shop[i]));
if(pv[i]!=-1)take=min(take,abs(shop[y]-play[pv[i]])+abs(shop[i]-play[pv[i]]));
if(nx[i]<play.size())take=min(take,abs(shop[y]-play[nx[i]])+abs(shop[i]-play[nx[i]]));
if(y!=i)ans=max(ans,dp(cur+take,y)+x);
else ans=max(ans,x);
}
if(x>0)cur+=2*sd;
}
//pf("dp %d %d: %d\n",t,i,ans);
return memo[t][i]=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;
}
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));
}
Compilation message
tortoise.cpp: In function 'int dp(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:26:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int y=i;y<shop.size();++y){
| ~^~~~~~~~~~~~
tortoise.cpp:30:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if(nx[i]<play.size())take=min(take,abs(shop[y]-play[nx[i]])+abs(shop[i]-play[nx[i]]));
| ~~~~~^~~~~~~~~~~~
tortoise.cpp:18:6: warning: unused variable 'num' [-Wunused-variable]
18 | int num=0;
| ^~~
tortoise.cpp: In function 'int main()':
tortoise.cpp:53:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int x=0;x<shop.size();++x){
| ~^~~~~~~~~~~~
tortoise.cpp:55:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | while(cur+1<play.size()&&play[cur+1]<i)++cur;
| ~~~~~^~~~~~~~~~~~
tortoise.cpp:41:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | sf("%d",&n);
| ^
tortoise.cpp:44:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | sf("%d",&a[i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
952 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
952 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
948 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
956 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
956 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
948 KB |
Output is correct |
23 |
Correct |
1 ms |
944 KB |
Output is correct |
24 |
Correct |
1 ms |
948 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
952 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
952 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
948 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
956 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
956 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
948 KB |
Output is correct |
23 |
Correct |
1 ms |
944 KB |
Output is correct |
24 |
Correct |
1 ms |
948 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
16 ms |
1048 KB |
Output is correct |
28 |
Correct |
40 ms |
980 KB |
Output is correct |
29 |
Correct |
21 ms |
980 KB |
Output is correct |
30 |
Correct |
19 ms |
980 KB |
Output is correct |
31 |
Correct |
20 ms |
980 KB |
Output is correct |
32 |
Correct |
9 ms |
1060 KB |
Output is correct |
33 |
Correct |
23 ms |
980 KB |
Output is correct |
34 |
Correct |
16 ms |
948 KB |
Output is correct |
35 |
Correct |
24 ms |
1036 KB |
Output is correct |
36 |
Correct |
20 ms |
952 KB |
Output is correct |
37 |
Correct |
17 ms |
980 KB |
Output is correct |
38 |
Correct |
18 ms |
1056 KB |
Output is correct |
39 |
Correct |
20 ms |
980 KB |
Output is correct |
40 |
Correct |
16 ms |
980 KB |
Output is correct |
41 |
Correct |
14 ms |
1056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
952 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
952 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
948 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
956 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
956 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
948 KB |
Output is correct |
23 |
Correct |
1 ms |
944 KB |
Output is correct |
24 |
Correct |
1 ms |
948 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
16 ms |
1048 KB |
Output is correct |
28 |
Correct |
40 ms |
980 KB |
Output is correct |
29 |
Correct |
21 ms |
980 KB |
Output is correct |
30 |
Correct |
19 ms |
980 KB |
Output is correct |
31 |
Correct |
20 ms |
980 KB |
Output is correct |
32 |
Correct |
9 ms |
1060 KB |
Output is correct |
33 |
Correct |
23 ms |
980 KB |
Output is correct |
34 |
Correct |
16 ms |
948 KB |
Output is correct |
35 |
Correct |
24 ms |
1036 KB |
Output is correct |
36 |
Correct |
20 ms |
952 KB |
Output is correct |
37 |
Correct |
17 ms |
980 KB |
Output is correct |
38 |
Correct |
18 ms |
1056 KB |
Output is correct |
39 |
Correct |
20 ms |
980 KB |
Output is correct |
40 |
Correct |
16 ms |
980 KB |
Output is correct |
41 |
Correct |
14 ms |
1056 KB |
Output is correct |
42 |
Correct |
7 ms |
948 KB |
Output is correct |
43 |
Correct |
9 ms |
1052 KB |
Output is correct |
44 |
Correct |
15 ms |
980 KB |
Output is correct |
45 |
Correct |
22 ms |
980 KB |
Output is correct |
46 |
Correct |
10 ms |
980 KB |
Output is correct |
47 |
Correct |
10 ms |
980 KB |
Output is correct |
48 |
Correct |
24 ms |
952 KB |
Output is correct |
49 |
Correct |
14 ms |
1052 KB |
Output is correct |
50 |
Correct |
80 ms |
1040 KB |
Output is correct |
51 |
Correct |
99 ms |
1040 KB |
Output is correct |
52 |
Correct |
37 ms |
1032 KB |
Output is correct |
53 |
Correct |
115 ms |
940 KB |
Output is correct |
54 |
Correct |
195 ms |
1036 KB |
Output is correct |
55 |
Correct |
85 ms |
1052 KB |
Output is correct |
56 |
Correct |
5 ms |
1108 KB |
Output is correct |
57 |
Correct |
16 ms |
1056 KB |
Output is correct |
58 |
Correct |
39 ms |
1044 KB |
Output is correct |
59 |
Correct |
22 ms |
980 KB |
Output is correct |
60 |
Correct |
67 ms |
1040 KB |
Output is correct |
61 |
Correct |
27 ms |
1040 KB |
Output is correct |
62 |
Correct |
53 ms |
1040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
952 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
952 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
948 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
956 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
956 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
948 KB |
Output is correct |
23 |
Correct |
1 ms |
944 KB |
Output is correct |
24 |
Correct |
1 ms |
948 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
16 ms |
1048 KB |
Output is correct |
28 |
Correct |
40 ms |
980 KB |
Output is correct |
29 |
Correct |
21 ms |
980 KB |
Output is correct |
30 |
Correct |
19 ms |
980 KB |
Output is correct |
31 |
Correct |
20 ms |
980 KB |
Output is correct |
32 |
Correct |
9 ms |
1060 KB |
Output is correct |
33 |
Correct |
23 ms |
980 KB |
Output is correct |
34 |
Correct |
16 ms |
948 KB |
Output is correct |
35 |
Correct |
24 ms |
1036 KB |
Output is correct |
36 |
Correct |
20 ms |
952 KB |
Output is correct |
37 |
Correct |
17 ms |
980 KB |
Output is correct |
38 |
Correct |
18 ms |
1056 KB |
Output is correct |
39 |
Correct |
20 ms |
980 KB |
Output is correct |
40 |
Correct |
16 ms |
980 KB |
Output is correct |
41 |
Correct |
14 ms |
1056 KB |
Output is correct |
42 |
Correct |
7 ms |
948 KB |
Output is correct |
43 |
Correct |
9 ms |
1052 KB |
Output is correct |
44 |
Correct |
15 ms |
980 KB |
Output is correct |
45 |
Correct |
22 ms |
980 KB |
Output is correct |
46 |
Correct |
10 ms |
980 KB |
Output is correct |
47 |
Correct |
10 ms |
980 KB |
Output is correct |
48 |
Correct |
24 ms |
952 KB |
Output is correct |
49 |
Correct |
14 ms |
1052 KB |
Output is correct |
50 |
Correct |
80 ms |
1040 KB |
Output is correct |
51 |
Correct |
99 ms |
1040 KB |
Output is correct |
52 |
Correct |
37 ms |
1032 KB |
Output is correct |
53 |
Correct |
115 ms |
940 KB |
Output is correct |
54 |
Correct |
195 ms |
1036 KB |
Output is correct |
55 |
Correct |
85 ms |
1052 KB |
Output is correct |
56 |
Correct |
5 ms |
1108 KB |
Output is correct |
57 |
Correct |
16 ms |
1056 KB |
Output is correct |
58 |
Correct |
39 ms |
1044 KB |
Output is correct |
59 |
Correct |
22 ms |
980 KB |
Output is correct |
60 |
Correct |
67 ms |
1040 KB |
Output is correct |
61 |
Correct |
27 ms |
1040 KB |
Output is correct |
62 |
Correct |
53 ms |
1040 KB |
Output is correct |
63 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
952 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
952 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
948 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
956 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
956 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
948 KB |
Output is correct |
23 |
Correct |
1 ms |
944 KB |
Output is correct |
24 |
Correct |
1 ms |
948 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
16 ms |
1048 KB |
Output is correct |
28 |
Correct |
40 ms |
980 KB |
Output is correct |
29 |
Correct |
21 ms |
980 KB |
Output is correct |
30 |
Correct |
19 ms |
980 KB |
Output is correct |
31 |
Correct |
20 ms |
980 KB |
Output is correct |
32 |
Correct |
9 ms |
1060 KB |
Output is correct |
33 |
Correct |
23 ms |
980 KB |
Output is correct |
34 |
Correct |
16 ms |
948 KB |
Output is correct |
35 |
Correct |
24 ms |
1036 KB |
Output is correct |
36 |
Correct |
20 ms |
952 KB |
Output is correct |
37 |
Correct |
17 ms |
980 KB |
Output is correct |
38 |
Correct |
18 ms |
1056 KB |
Output is correct |
39 |
Correct |
20 ms |
980 KB |
Output is correct |
40 |
Correct |
16 ms |
980 KB |
Output is correct |
41 |
Correct |
14 ms |
1056 KB |
Output is correct |
42 |
Correct |
7 ms |
948 KB |
Output is correct |
43 |
Correct |
9 ms |
1052 KB |
Output is correct |
44 |
Correct |
15 ms |
980 KB |
Output is correct |
45 |
Correct |
22 ms |
980 KB |
Output is correct |
46 |
Correct |
10 ms |
980 KB |
Output is correct |
47 |
Correct |
10 ms |
980 KB |
Output is correct |
48 |
Correct |
24 ms |
952 KB |
Output is correct |
49 |
Correct |
14 ms |
1052 KB |
Output is correct |
50 |
Correct |
80 ms |
1040 KB |
Output is correct |
51 |
Correct |
99 ms |
1040 KB |
Output is correct |
52 |
Correct |
37 ms |
1032 KB |
Output is correct |
53 |
Correct |
115 ms |
940 KB |
Output is correct |
54 |
Correct |
195 ms |
1036 KB |
Output is correct |
55 |
Correct |
85 ms |
1052 KB |
Output is correct |
56 |
Correct |
5 ms |
1108 KB |
Output is correct |
57 |
Correct |
16 ms |
1056 KB |
Output is correct |
58 |
Correct |
39 ms |
1044 KB |
Output is correct |
59 |
Correct |
22 ms |
980 KB |
Output is correct |
60 |
Correct |
67 ms |
1040 KB |
Output is correct |
61 |
Correct |
27 ms |
1040 KB |
Output is correct |
62 |
Correct |
53 ms |
1040 KB |
Output is correct |
63 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |