#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define pb push_back
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
const int mxn=405;
int dp[mxn][mxn][mxn/2][2];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
setio();
int n,l;
cin>>n>>l;
vector<pii>temp;
vector<int>pos(2*n+1);
vector<int>time(2*n+1);
temp.pb({0,0});
for(int i=0;i<n;i++){
int x;
cin>>x;
temp.pb({x,0});
}
for(int i=0;i<n;i++){
int t;
cin>>t;
temp[i+1].S=t;
}
sort(all(temp));
for(int i=1;i<=n;i++){
time[i]=time[i+n]=temp[i].S;
pos[i]=temp[i].F;
pos[i+n]=temp[i].F+l;
}
for(int i=0;i<=2*n;i++){
for(int j=0;j<=2*n;j++){
for(int k=0;k<=2*n;k++){
dp[i][j][k][0]=dp[i][j][k][1]=2e9;
}
}
}
int ans=0;
for(int i=1;i<=2*n;i++){
if(abs(pos[i]-l)<=time[i]){
dp[i][i][1][0]=dp[i][i][1][1]=abs(pos[i]-l);
ans=1;
}
}
for(int k=1;k<=n;k++){
for(int l=2*n;l>=1;l--){
for(int r=l+1;r<=2*n;r++){
if(l==r)continue;
dp[l][r][k][0]=min(dp[l+1][r][k][0]+abs(pos[l]-pos[l+1]),
dp[l+1][r][k][1]+abs(pos[l]-pos[r]));
if(dp[l+1][r][k-1][0]+abs(pos[l]-pos[l+1])<=time[l])dp[l][r][k][0]=min(dp[l][r][k][0],dp[l+1][r][k-1][0]+abs(pos[l]-pos[l+1]));
if(dp[l+1][r][k-1][1]+abs(pos[l]-pos[ r])<=time[l])dp[l][r][k][0]=min(dp[l][r][k][0],dp[l+1][r][k-1][1]+abs(pos[l]-pos[r]));
dp[l][r][k][1]=min(dp[l][r-1][k][0]+abs(pos[r ]-pos[l]),
dp[l][r-1][k][1]+abs(pos[r-1]-pos[r]));
if(dp[l][r-1][k-1][0]+abs(pos[l]-pos[ r])<=time[r])dp[l][r][k][1]=min(dp[l][r][k][1],dp[l][r-1][k-1][0]+abs(pos[l]-pos[r]));
if(dp[l][r-1][k-1][1]+abs(pos[r-1]-pos[r])<=time[r])dp[l][r][k][1]=min(dp[l][r][k][1],dp[l][r-1][k-1][1]+abs(pos[r-1]-pos[r]));
dp[l][r][k][0]=min(dp[l][r][k][0],(int)2e9);
dp[l][r][k][1]=min(dp[l][r][k][1],(int)2e9);
if(r-l+1<=n and (dp[l][r][k][0]<2e9 or dp[l][r][k][1]<2e9)){
ans=max(ans,k);
}
//cout<<l<<' '<<r<<' '<<k<<' '<<dp[l][r][k][0]<<' '<<dp[l][r][k][1]<<'\n';
}
}
}
cout<<ans<<'\n';
}
/*
dp[l][r][k][0/1] min time to collect k stamps between lth~rth stamp 0->ends at l 1->ends at r
dp[l][r][k][0]=if(dp[l+1][r][k-1][0]+x[l]-x[l-1]<=t[l])dp[l-1][r][k-1][0]+x[l]-x[l-1]
if(dp[l+1][r][k-1][1]+x[l]-x[r]<=t[l])
dp[l-1][r][k][0]+x[l]-x[l-1]
dp[l-1][r][k][1]+x[l]-x[r]
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
4 ms |
24924 KB |
Output is correct |
4 |
Correct |
2 ms |
16892 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
4 ms |
31068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
4 ms |
24924 KB |
Output is correct |
4 |
Correct |
2 ms |
16892 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
4 ms |
31068 KB |
Output is correct |
18 |
Correct |
5 ms |
37468 KB |
Output is correct |
19 |
Correct |
3 ms |
26968 KB |
Output is correct |
20 |
Correct |
4 ms |
31064 KB |
Output is correct |
21 |
Correct |
5 ms |
37468 KB |
Output is correct |
22 |
Correct |
4 ms |
24924 KB |
Output is correct |
23 |
Correct |
5 ms |
39516 KB |
Output is correct |
24 |
Correct |
4 ms |
35420 KB |
Output is correct |
25 |
Correct |
4 ms |
37468 KB |
Output is correct |
26 |
Correct |
5 ms |
37468 KB |
Output is correct |
27 |
Correct |
3 ms |
21016 KB |
Output is correct |
28 |
Correct |
3 ms |
25108 KB |
Output is correct |
29 |
Correct |
5 ms |
39512 KB |
Output is correct |
30 |
Correct |
5 ms |
39516 KB |
Output is correct |
31 |
Correct |
5 ms |
37468 KB |
Output is correct |
32 |
Correct |
4 ms |
35416 KB |
Output is correct |
33 |
Correct |
5 ms |
39516 KB |
Output is correct |
34 |
Correct |
4 ms |
39380 KB |
Output is correct |
35 |
Correct |
5 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
4 ms |
24924 KB |
Output is correct |
4 |
Correct |
2 ms |
16892 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
4 ms |
31068 KB |
Output is correct |
18 |
Correct |
265 ms |
424276 KB |
Output is correct |
19 |
Correct |
137 ms |
287740 KB |
Output is correct |
20 |
Correct |
63 ms |
195668 KB |
Output is correct |
21 |
Correct |
186 ms |
276504 KB |
Output is correct |
22 |
Correct |
179 ms |
334164 KB |
Output is correct |
23 |
Correct |
54 ms |
181072 KB |
Output is correct |
24 |
Correct |
41 ms |
163420 KB |
Output is correct |
25 |
Correct |
52 ms |
179284 KB |
Output is correct |
26 |
Correct |
20 ms |
133976 KB |
Output is correct |
27 |
Correct |
54 ms |
182620 KB |
Output is correct |
28 |
Correct |
39 ms |
159068 KB |
Output is correct |
29 |
Correct |
54 ms |
184404 KB |
Output is correct |
30 |
Correct |
42 ms |
166480 KB |
Output is correct |
31 |
Correct |
53 ms |
179280 KB |
Output is correct |
32 |
Correct |
27 ms |
143188 KB |
Output is correct |
33 |
Correct |
51 ms |
179280 KB |
Output is correct |
34 |
Correct |
19 ms |
132440 KB |
Output is correct |
35 |
Correct |
51 ms |
177656 KB |
Output is correct |
36 |
Correct |
24 ms |
139344 KB |
Output is correct |
37 |
Correct |
56 ms |
182608 KB |
Output is correct |
38 |
Correct |
29 ms |
146524 KB |
Output is correct |
39 |
Correct |
55 ms |
186196 KB |
Output is correct |
40 |
Correct |
35 ms |
151400 KB |
Output is correct |
41 |
Correct |
353 ms |
507732 KB |
Output is correct |
42 |
Correct |
208 ms |
364920 KB |
Output is correct |
43 |
Correct |
366 ms |
507968 KB |
Output is correct |
44 |
Correct |
199 ms |
361272 KB |
Output is correct |
45 |
Correct |
378 ms |
507936 KB |
Output is correct |
46 |
Correct |
204 ms |
364956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
4 ms |
24924 KB |
Output is correct |
4 |
Correct |
2 ms |
16892 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
26972 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
4 ms |
31068 KB |
Output is correct |
18 |
Correct |
5 ms |
37468 KB |
Output is correct |
19 |
Correct |
3 ms |
26968 KB |
Output is correct |
20 |
Correct |
4 ms |
31064 KB |
Output is correct |
21 |
Correct |
5 ms |
37468 KB |
Output is correct |
22 |
Correct |
4 ms |
24924 KB |
Output is correct |
23 |
Correct |
5 ms |
39516 KB |
Output is correct |
24 |
Correct |
4 ms |
35420 KB |
Output is correct |
25 |
Correct |
4 ms |
37468 KB |
Output is correct |
26 |
Correct |
5 ms |
37468 KB |
Output is correct |
27 |
Correct |
3 ms |
21016 KB |
Output is correct |
28 |
Correct |
3 ms |
25108 KB |
Output is correct |
29 |
Correct |
5 ms |
39512 KB |
Output is correct |
30 |
Correct |
5 ms |
39516 KB |
Output is correct |
31 |
Correct |
5 ms |
37468 KB |
Output is correct |
32 |
Correct |
4 ms |
35416 KB |
Output is correct |
33 |
Correct |
5 ms |
39516 KB |
Output is correct |
34 |
Correct |
4 ms |
39380 KB |
Output is correct |
35 |
Correct |
5 ms |
39516 KB |
Output is correct |
36 |
Correct |
265 ms |
424276 KB |
Output is correct |
37 |
Correct |
137 ms |
287740 KB |
Output is correct |
38 |
Correct |
63 ms |
195668 KB |
Output is correct |
39 |
Correct |
186 ms |
276504 KB |
Output is correct |
40 |
Correct |
179 ms |
334164 KB |
Output is correct |
41 |
Correct |
54 ms |
181072 KB |
Output is correct |
42 |
Correct |
41 ms |
163420 KB |
Output is correct |
43 |
Correct |
52 ms |
179284 KB |
Output is correct |
44 |
Correct |
20 ms |
133976 KB |
Output is correct |
45 |
Correct |
54 ms |
182620 KB |
Output is correct |
46 |
Correct |
39 ms |
159068 KB |
Output is correct |
47 |
Correct |
54 ms |
184404 KB |
Output is correct |
48 |
Correct |
42 ms |
166480 KB |
Output is correct |
49 |
Correct |
53 ms |
179280 KB |
Output is correct |
50 |
Correct |
27 ms |
143188 KB |
Output is correct |
51 |
Correct |
51 ms |
179280 KB |
Output is correct |
52 |
Correct |
19 ms |
132440 KB |
Output is correct |
53 |
Correct |
51 ms |
177656 KB |
Output is correct |
54 |
Correct |
24 ms |
139344 KB |
Output is correct |
55 |
Correct |
56 ms |
182608 KB |
Output is correct |
56 |
Correct |
29 ms |
146524 KB |
Output is correct |
57 |
Correct |
55 ms |
186196 KB |
Output is correct |
58 |
Correct |
35 ms |
151400 KB |
Output is correct |
59 |
Correct |
353 ms |
507732 KB |
Output is correct |
60 |
Correct |
208 ms |
364920 KB |
Output is correct |
61 |
Correct |
366 ms |
507968 KB |
Output is correct |
62 |
Correct |
199 ms |
361272 KB |
Output is correct |
63 |
Correct |
378 ms |
507936 KB |
Output is correct |
64 |
Correct |
204 ms |
364956 KB |
Output is correct |
65 |
Correct |
306 ms |
464936 KB |
Output is correct |
66 |
Correct |
281 ms |
432084 KB |
Output is correct |
67 |
Correct |
256 ms |
416588 KB |
Output is correct |
68 |
Correct |
226 ms |
390184 KB |
Output is correct |
69 |
Correct |
304 ms |
460628 KB |
Output is correct |
70 |
Correct |
285 ms |
444380 KB |
Output is correct |
71 |
Correct |
292 ms |
448452 KB |
Output is correct |
72 |
Correct |
292 ms |
452432 KB |
Output is correct |
73 |
Correct |
269 ms |
424564 KB |
Output is correct |
74 |
Correct |
268 ms |
401392 KB |
Output is correct |
75 |
Correct |
277 ms |
436308 KB |
Output is correct |
76 |
Correct |
350 ms |
490244 KB |
Output is correct |
77 |
Correct |
343 ms |
490288 KB |
Output is correct |
78 |
Correct |
260 ms |
393264 KB |
Output is correct |
79 |
Correct |
241 ms |
404616 KB |
Output is correct |
80 |
Correct |
339 ms |
481648 KB |
Output is correct |
81 |
Correct |
249 ms |
408128 KB |
Output is correct |
82 |
Correct |
280 ms |
428128 KB |
Output is correct |
83 |
Correct |
364 ms |
507664 KB |
Output is correct |
84 |
Correct |
282 ms |
444168 KB |
Output is correct |
85 |
Correct |
329 ms |
477864 KB |
Output is correct |
86 |
Correct |
307 ms |
469104 KB |
Output is correct |
87 |
Correct |
275 ms |
436096 KB |
Output is correct |
88 |
Correct |
368 ms |
512324 KB |
Output is correct |
89 |
Correct |
404 ms |
512596 KB |
Output is correct |
90 |
Correct |
283 ms |
440276 KB |
Output is correct |
91 |
Correct |
375 ms |
512248 KB |
Output is correct |
92 |
Correct |
363 ms |
512392 KB |
Output is correct |
93 |
Correct |
361 ms |
503676 KB |
Output is correct |