#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long n,l;
long long ans = 0;
const long long MAXN = 210;
long long dp[MAXN][MAXN][MAXN][5];
long long arr[MAXN];
long long t[MAXN];
long long check(long long x){
x+=(n+1);
x%=(n+1);
return x;
}
long long dist(long long from,long long to,long long x){
if(arr[to]>=arr[from]){
x=arr[to]-arr[from];
}
return min(abs(arr[to]-arr[from]),l-abs(arr[from]-arr[to]));
}
int main(){
cin>>n>>l;
for(long long i=0;i<=n;i++){
for(long long j=0;j<=n;j++){
for(long long k=0;k<=n;k++){
for(long long l=0;l<=4;l++){
dp[i][j][k][l] = 1e14;
}
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
for(long long i=1;i<=n;i++){
cin>>arr[i];
}
for(long long i=1;i<=n;i++){
cin>>t[i];
}
t[0] = -1e9;
for(long long i=0;i<=n;i++){
for(long long len=0;len<=n;len++){
for(long long l=0;l<=n;l++){
long long r=check(l+len);
if(len == 0 && l==0){
r=0;
}else if(len == 0){
continue;
}
dp[i][l][r][0] = min(dp[i][l][r][0],dp[i][l][r][1]+dist(r,l,1));
dp[i][l][r][1] = min(dp[i][l][r][1],dp[i][l][r][0]+dist(l,r,0));
dp[i][l][r][0] = min(dp[i][l][r][0],dp[i][l][r][1]+dist(r,l,1));
dp[i][l][r][1] = min(dp[i][l][r][1],dp[i][l][r][0]+dist(l,r,0));
long long currl = dp[i][l][r][0];
long long currr = dp[i][l][r][1];
// cout<<len<<" "<<l<<" "<<r<<endl;
if(currl>=1e14 && currr >=1e14){
continue;
}
ans = max(ans,i);
if(i==1){
// cout<<l<<" "<<r<<" "<<currl<<" "<<currr<<endl;
}
// cout<<currl<<" "<<currr<<endl;
//int nxt = min(dist(l,check(r+1),0))
if(currl+dist(l,check(r+1),0)<=t[check(r+1)]){
dp[i+1][l][check(r+1)][1] = min(dp[i+1][l][check(r+1)][1],currl+dist(l,check(r+1),0));
}else{
dp[i][l][check(r+1)][1] = min(dp[i][l][check(r+1)][1],currl+dist(l,check(r+1),0));
}
if(currr+dist(r,check(r+1),0)<=t[check(r+1)]){
dp[i+1][l][check(r+1)][1] = min(dp[i+1][l][check(r+1)][1],currr+dist(r,check(r+1),0));
}else{
dp[i][l][check(r+1)][1] = min(dp[i][l][check(r+1)][1],currr+dist(r,check(r+1),0));
}
if(currl+dist(l,check(l-1),1)<=t[check(l-1)]){
dp[i+1][check(l-1)][r][0]=min(dp[i+1][check(l-1)][r][0],currl+dist(l,check(l-1),1));
//cout<<i+1<<" "<<check(l-1)<<" "<<r<<endl;
}else{
dp[i][check(l-1)][r][0]= min(dp[i][check(l-1)][r][0],currl+dist(l,check(l-1),1));
}
if(currr+dist(r,check(l-1),1)<=t[check(l-1)]){
dp[i+1][check(l-1)][r][0] = min(dp[i+1][check(l-1)][r][0],currr+dist(r,check(l-1),1));
// cout<<i+1<<endl;
// cout<<check(l-1)<<" "<<r<<" "<<dp[i+1][check(l-1)][r][0]<<endl;
}else{
dp[i][check(l-1)][r][0] = min(dp[i][check(l-1)][r][0],currr+dist(r,check(l-1),1));
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=0;j<=n;j++){
for(int k=0;k<=n;k++){
if(dp[i][j][k][0]!=1e14||dp[i][j][k][1]!=1e14){
// cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k][0]<<" "<<dp[i][j][k][1]<<endl;;
}
}
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
5 ms |
1152 KB |
Output is correct |
18 |
Correct |
5 ms |
1408 KB |
Output is correct |
19 |
Correct |
5 ms |
896 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
5 ms |
1408 KB |
Output is correct |
22 |
Correct |
5 ms |
768 KB |
Output is correct |
23 |
Correct |
5 ms |
1536 KB |
Output is correct |
24 |
Correct |
6 ms |
1280 KB |
Output is correct |
25 |
Correct |
5 ms |
1408 KB |
Output is correct |
26 |
Correct |
5 ms |
1408 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
5 ms |
768 KB |
Output is correct |
29 |
Correct |
5 ms |
1536 KB |
Output is correct |
30 |
Correct |
5 ms |
1536 KB |
Output is correct |
31 |
Correct |
5 ms |
1408 KB |
Output is correct |
32 |
Correct |
5 ms |
1280 KB |
Output is correct |
33 |
Correct |
5 ms |
1536 KB |
Output is correct |
34 |
Correct |
5 ms |
1536 KB |
Output is correct |
35 |
Correct |
5 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
5 ms |
1152 KB |
Output is correct |
18 |
Correct |
294 ms |
267400 KB |
Output is correct |
19 |
Correct |
128 ms |
161784 KB |
Output is correct |
20 |
Correct |
63 ms |
83576 KB |
Output is correct |
21 |
Correct |
117 ms |
152696 KB |
Output is correct |
22 |
Correct |
225 ms |
198264 KB |
Output is correct |
23 |
Correct |
53 ms |
68344 KB |
Output is correct |
24 |
Correct |
38 ms |
50432 KB |
Output is correct |
25 |
Correct |
52 ms |
66552 KB |
Output is correct |
26 |
Correct |
19 ms |
20736 KB |
Output is correct |
27 |
Correct |
55 ms |
70144 KB |
Output is correct |
28 |
Correct |
58 ms |
46080 KB |
Output is correct |
29 |
Correct |
55 ms |
71928 KB |
Output is correct |
30 |
Correct |
42 ms |
53496 KB |
Output is correct |
31 |
Correct |
51 ms |
66560 KB |
Output is correct |
32 |
Correct |
25 ms |
30208 KB |
Output is correct |
33 |
Correct |
53 ms |
66560 KB |
Output is correct |
34 |
Correct |
18 ms |
19072 KB |
Output is correct |
35 |
Correct |
47 ms |
64760 KB |
Output is correct |
36 |
Correct |
21 ms |
26112 KB |
Output is correct |
37 |
Correct |
51 ms |
70136 KB |
Output is correct |
38 |
Correct |
28 ms |
33536 KB |
Output is correct |
39 |
Correct |
55 ms |
73848 KB |
Output is correct |
40 |
Correct |
29 ms |
38272 KB |
Output is correct |
41 |
Correct |
305 ms |
329848 KB |
Output is correct |
42 |
Correct |
169 ms |
221924 KB |
Output is correct |
43 |
Correct |
305 ms |
329848 KB |
Output is correct |
44 |
Correct |
166 ms |
219256 KB |
Output is correct |
45 |
Correct |
299 ms |
329880 KB |
Output is correct |
46 |
Correct |
171 ms |
221816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
5 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
1152 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
1152 KB |
Output is correct |
17 |
Correct |
5 ms |
1152 KB |
Output is correct |
18 |
Correct |
5 ms |
1408 KB |
Output is correct |
19 |
Correct |
5 ms |
896 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
5 ms |
1408 KB |
Output is correct |
22 |
Correct |
5 ms |
768 KB |
Output is correct |
23 |
Correct |
5 ms |
1536 KB |
Output is correct |
24 |
Correct |
6 ms |
1280 KB |
Output is correct |
25 |
Correct |
5 ms |
1408 KB |
Output is correct |
26 |
Correct |
5 ms |
1408 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
5 ms |
768 KB |
Output is correct |
29 |
Correct |
5 ms |
1536 KB |
Output is correct |
30 |
Correct |
5 ms |
1536 KB |
Output is correct |
31 |
Correct |
5 ms |
1408 KB |
Output is correct |
32 |
Correct |
5 ms |
1280 KB |
Output is correct |
33 |
Correct |
5 ms |
1536 KB |
Output is correct |
34 |
Correct |
5 ms |
1536 KB |
Output is correct |
35 |
Correct |
5 ms |
1536 KB |
Output is correct |
36 |
Correct |
294 ms |
267400 KB |
Output is correct |
37 |
Correct |
128 ms |
161784 KB |
Output is correct |
38 |
Correct |
63 ms |
83576 KB |
Output is correct |
39 |
Correct |
117 ms |
152696 KB |
Output is correct |
40 |
Correct |
225 ms |
198264 KB |
Output is correct |
41 |
Correct |
53 ms |
68344 KB |
Output is correct |
42 |
Correct |
38 ms |
50432 KB |
Output is correct |
43 |
Correct |
52 ms |
66552 KB |
Output is correct |
44 |
Correct |
19 ms |
20736 KB |
Output is correct |
45 |
Correct |
55 ms |
70144 KB |
Output is correct |
46 |
Correct |
58 ms |
46080 KB |
Output is correct |
47 |
Correct |
55 ms |
71928 KB |
Output is correct |
48 |
Correct |
42 ms |
53496 KB |
Output is correct |
49 |
Correct |
51 ms |
66560 KB |
Output is correct |
50 |
Correct |
25 ms |
30208 KB |
Output is correct |
51 |
Correct |
53 ms |
66560 KB |
Output is correct |
52 |
Correct |
18 ms |
19072 KB |
Output is correct |
53 |
Correct |
47 ms |
64760 KB |
Output is correct |
54 |
Correct |
21 ms |
26112 KB |
Output is correct |
55 |
Correct |
51 ms |
70136 KB |
Output is correct |
56 |
Correct |
28 ms |
33536 KB |
Output is correct |
57 |
Correct |
55 ms |
73848 KB |
Output is correct |
58 |
Correct |
29 ms |
38272 KB |
Output is correct |
59 |
Correct |
305 ms |
329848 KB |
Output is correct |
60 |
Correct |
169 ms |
221924 KB |
Output is correct |
61 |
Correct |
305 ms |
329848 KB |
Output is correct |
62 |
Correct |
166 ms |
219256 KB |
Output is correct |
63 |
Correct |
299 ms |
329880 KB |
Output is correct |
64 |
Correct |
171 ms |
221816 KB |
Output is correct |
65 |
Correct |
260 ms |
297720 KB |
Output is correct |
66 |
Correct |
242 ms |
273156 KB |
Output is correct |
67 |
Correct |
229 ms |
261372 KB |
Output is correct |
68 |
Correct |
204 ms |
241144 KB |
Output is correct |
69 |
Correct |
254 ms |
294648 KB |
Output is correct |
70 |
Correct |
257 ms |
282472 KB |
Output is correct |
71 |
Correct |
240 ms |
285304 KB |
Output is correct |
72 |
Correct |
270 ms |
288508 KB |
Output is correct |
73 |
Correct |
226 ms |
267256 KB |
Output is correct |
74 |
Correct |
219 ms |
249720 KB |
Output is correct |
75 |
Correct |
245 ms |
276344 KB |
Output is correct |
76 |
Correct |
314 ms |
316792 KB |
Output is correct |
77 |
Correct |
275 ms |
316792 KB |
Output is correct |
78 |
Correct |
232 ms |
244088 KB |
Output is correct |
79 |
Correct |
216 ms |
252540 KB |
Output is correct |
80 |
Correct |
315 ms |
310520 KB |
Output is correct |
81 |
Correct |
219 ms |
255480 KB |
Output is correct |
82 |
Correct |
229 ms |
270200 KB |
Output is correct |
83 |
Correct |
306 ms |
329848 KB |
Output is correct |
84 |
Correct |
241 ms |
282232 KB |
Output is correct |
85 |
Correct |
261 ms |
307192 KB |
Output is correct |
86 |
Correct |
269 ms |
300924 KB |
Output is correct |
87 |
Correct |
256 ms |
276216 KB |
Output is correct |
88 |
Correct |
328 ms |
333304 KB |
Output is correct |
89 |
Correct |
303 ms |
333176 KB |
Output is correct |
90 |
Correct |
224 ms |
279288 KB |
Output is correct |
91 |
Correct |
328 ms |
333256 KB |
Output is correct |
92 |
Correct |
306 ms |
333304 KB |
Output is correct |
93 |
Correct |
273 ms |
326648 KB |
Output is correct |