#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pll pair<ll,ll>
#define fs first
#define sc second
const ll mxn = 202;
ll dp[mxn][mxn][mxn][2];
bool done[mxn][mxn][mxn][2];
pll arr[mxn];
priority_queue<tuple<ll,ll,ll,ll,ll>,vector<tuple<ll,ll,ll,ll,ll>>,greater<tuple<ll,ll,ll,ll,ll>>> pq;
ll n,L;
const ll inf = 1e9+10;
ll calc(ll s,ll e,ll dir){
if(dir){
return (e+L-s)%(L);
}
else{
return L-(e+L-s)%L;
}
}
void solve(){
cin>>n>>L;
for(int i = 1;i<=n;i++)cin>>arr[i].fs;
for(int i = 1;i<=n;i++)cin>>arr[i].sc;
for(auto &i:dp)for(auto &j:i)for(auto &k:j)for(auto &l:k)l = inf;
arr[0] = {0,0};
dp[0][0][0][0] = 0;
pq.push(make_tuple(0,0,0,0,0));
ll ans = 0;
while(!pq.empty()){
ll val = get<0>(pq.top()),l = get<1>(pq.top()),r = get<2>(pq.top());
ll cnt = get<3>(pq.top()),dir = get<4>(pq.top());
//cout<<val<<' '<<l<<' '<<r<<' '<<cnt<<' '<<dir<<endl;
pq.pop();
if(dp[l][r][cnt][dir] != val)continue;
if((r+1)%(n+1) == l){
ans = max(ans,cnt);
continue;
}
ll pos = (dir?arr[r].fs:arr[l].fs);
int nr = r+1;
if(nr>=n+1)nr -= n+1;
int nl = l-1;
if(nl<0)nl += n+1;
ll rt = val+calc(pos,arr[nr].fs,1);
ll rcnt = cnt + (rt<=arr[nr].sc?1:0);
if(dp[l][nr][rcnt][1] > rt)pq.push(make_tuple(rt,l,nr,rcnt,1)),dp[l][nr][rcnt][1] = rt;
ll lt = val+calc(pos,arr[nl].fs,0);
ll lcnt = cnt+(lt<=arr[nl].sc?1:0);
//cout<<l<<','<<r<<','<<dir<<','<<pos<<lt<<','<<lcnt<<','<<','<<nl<<endl;
if(dp[nl][r][lcnt][0] > lt)pq.push(make_tuple(lt,nl,r,lcnt,0)),dp[nl][r][lcnt][0] = lt;
}
cout<<ans;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
while(t--)solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
64716 KB |
Output is correct |
2 |
Correct |
28 ms |
64816 KB |
Output is correct |
3 |
Correct |
29 ms |
64828 KB |
Output is correct |
4 |
Correct |
30 ms |
64728 KB |
Output is correct |
5 |
Correct |
32 ms |
64716 KB |
Output is correct |
6 |
Correct |
29 ms |
64820 KB |
Output is correct |
7 |
Correct |
30 ms |
64760 KB |
Output is correct |
8 |
Correct |
30 ms |
64852 KB |
Output is correct |
9 |
Correct |
29 ms |
64816 KB |
Output is correct |
10 |
Correct |
29 ms |
64784 KB |
Output is correct |
11 |
Correct |
29 ms |
64736 KB |
Output is correct |
12 |
Correct |
30 ms |
64764 KB |
Output is correct |
13 |
Correct |
30 ms |
64752 KB |
Output is correct |
14 |
Correct |
30 ms |
64760 KB |
Output is correct |
15 |
Correct |
30 ms |
64824 KB |
Output is correct |
16 |
Correct |
36 ms |
64820 KB |
Output is correct |
17 |
Correct |
30 ms |
64836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
64716 KB |
Output is correct |
2 |
Correct |
28 ms |
64816 KB |
Output is correct |
3 |
Correct |
29 ms |
64828 KB |
Output is correct |
4 |
Correct |
30 ms |
64728 KB |
Output is correct |
5 |
Correct |
32 ms |
64716 KB |
Output is correct |
6 |
Correct |
29 ms |
64820 KB |
Output is correct |
7 |
Correct |
30 ms |
64760 KB |
Output is correct |
8 |
Correct |
30 ms |
64852 KB |
Output is correct |
9 |
Correct |
29 ms |
64816 KB |
Output is correct |
10 |
Correct |
29 ms |
64784 KB |
Output is correct |
11 |
Correct |
29 ms |
64736 KB |
Output is correct |
12 |
Correct |
30 ms |
64764 KB |
Output is correct |
13 |
Correct |
30 ms |
64752 KB |
Output is correct |
14 |
Correct |
30 ms |
64760 KB |
Output is correct |
15 |
Correct |
30 ms |
64824 KB |
Output is correct |
16 |
Correct |
36 ms |
64820 KB |
Output is correct |
17 |
Correct |
30 ms |
64836 KB |
Output is correct |
18 |
Correct |
37 ms |
64768 KB |
Output is correct |
19 |
Correct |
30 ms |
64724 KB |
Output is correct |
20 |
Correct |
36 ms |
64812 KB |
Output is correct |
21 |
Correct |
33 ms |
64804 KB |
Output is correct |
22 |
Correct |
30 ms |
64764 KB |
Output is correct |
23 |
Correct |
31 ms |
64828 KB |
Output is correct |
24 |
Correct |
31 ms |
64852 KB |
Output is correct |
25 |
Correct |
30 ms |
64792 KB |
Output is correct |
26 |
Correct |
32 ms |
64804 KB |
Output is correct |
27 |
Correct |
32 ms |
64852 KB |
Output is correct |
28 |
Correct |
30 ms |
64768 KB |
Output is correct |
29 |
Correct |
31 ms |
64724 KB |
Output is correct |
30 |
Correct |
29 ms |
64852 KB |
Output is correct |
31 |
Correct |
39 ms |
64764 KB |
Output is correct |
32 |
Correct |
32 ms |
64756 KB |
Output is correct |
33 |
Correct |
32 ms |
64832 KB |
Output is correct |
34 |
Correct |
33 ms |
64764 KB |
Output is correct |
35 |
Correct |
31 ms |
64764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
64716 KB |
Output is correct |
2 |
Correct |
28 ms |
64816 KB |
Output is correct |
3 |
Correct |
29 ms |
64828 KB |
Output is correct |
4 |
Correct |
30 ms |
64728 KB |
Output is correct |
5 |
Correct |
32 ms |
64716 KB |
Output is correct |
6 |
Correct |
29 ms |
64820 KB |
Output is correct |
7 |
Correct |
30 ms |
64760 KB |
Output is correct |
8 |
Correct |
30 ms |
64852 KB |
Output is correct |
9 |
Correct |
29 ms |
64816 KB |
Output is correct |
10 |
Correct |
29 ms |
64784 KB |
Output is correct |
11 |
Correct |
29 ms |
64736 KB |
Output is correct |
12 |
Correct |
30 ms |
64764 KB |
Output is correct |
13 |
Correct |
30 ms |
64752 KB |
Output is correct |
14 |
Correct |
30 ms |
64760 KB |
Output is correct |
15 |
Correct |
30 ms |
64824 KB |
Output is correct |
16 |
Correct |
36 ms |
64820 KB |
Output is correct |
17 |
Correct |
30 ms |
64836 KB |
Output is correct |
18 |
Correct |
841 ms |
75284 KB |
Output is correct |
19 |
Correct |
388 ms |
70056 KB |
Output is correct |
20 |
Correct |
134 ms |
67424 KB |
Output is correct |
21 |
Correct |
301 ms |
70108 KB |
Output is correct |
22 |
Correct |
528 ms |
75168 KB |
Output is correct |
23 |
Correct |
151 ms |
67460 KB |
Output is correct |
24 |
Correct |
63 ms |
66224 KB |
Output is correct |
25 |
Correct |
130 ms |
67468 KB |
Output is correct |
26 |
Correct |
43 ms |
65324 KB |
Output is correct |
27 |
Correct |
158 ms |
67440 KB |
Output is correct |
28 |
Correct |
58 ms |
66208 KB |
Output is correct |
29 |
Correct |
125 ms |
67420 KB |
Output is correct |
30 |
Correct |
62 ms |
66208 KB |
Output is correct |
31 |
Correct |
119 ms |
67508 KB |
Output is correct |
32 |
Correct |
58 ms |
65540 KB |
Output is correct |
33 |
Correct |
116 ms |
66244 KB |
Output is correct |
34 |
Correct |
41 ms |
65540 KB |
Output is correct |
35 |
Correct |
49 ms |
65288 KB |
Output is correct |
36 |
Correct |
45 ms |
65492 KB |
Output is correct |
37 |
Correct |
55 ms |
65500 KB |
Output is correct |
38 |
Correct |
57 ms |
65524 KB |
Output is correct |
39 |
Correct |
56 ms |
65556 KB |
Output is correct |
40 |
Correct |
49 ms |
65724 KB |
Output is correct |
41 |
Correct |
1457 ms |
85596 KB |
Output is correct |
42 |
Correct |
38 ms |
65196 KB |
Output is correct |
43 |
Correct |
1421 ms |
85404 KB |
Output is correct |
44 |
Correct |
37 ms |
65076 KB |
Output is correct |
45 |
Correct |
1437 ms |
85476 KB |
Output is correct |
46 |
Correct |
38 ms |
65320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
64716 KB |
Output is correct |
2 |
Correct |
28 ms |
64816 KB |
Output is correct |
3 |
Correct |
29 ms |
64828 KB |
Output is correct |
4 |
Correct |
30 ms |
64728 KB |
Output is correct |
5 |
Correct |
32 ms |
64716 KB |
Output is correct |
6 |
Correct |
29 ms |
64820 KB |
Output is correct |
7 |
Correct |
30 ms |
64760 KB |
Output is correct |
8 |
Correct |
30 ms |
64852 KB |
Output is correct |
9 |
Correct |
29 ms |
64816 KB |
Output is correct |
10 |
Correct |
29 ms |
64784 KB |
Output is correct |
11 |
Correct |
29 ms |
64736 KB |
Output is correct |
12 |
Correct |
30 ms |
64764 KB |
Output is correct |
13 |
Correct |
30 ms |
64752 KB |
Output is correct |
14 |
Correct |
30 ms |
64760 KB |
Output is correct |
15 |
Correct |
30 ms |
64824 KB |
Output is correct |
16 |
Correct |
36 ms |
64820 KB |
Output is correct |
17 |
Correct |
30 ms |
64836 KB |
Output is correct |
18 |
Correct |
37 ms |
64768 KB |
Output is correct |
19 |
Correct |
30 ms |
64724 KB |
Output is correct |
20 |
Correct |
36 ms |
64812 KB |
Output is correct |
21 |
Correct |
33 ms |
64804 KB |
Output is correct |
22 |
Correct |
30 ms |
64764 KB |
Output is correct |
23 |
Correct |
31 ms |
64828 KB |
Output is correct |
24 |
Correct |
31 ms |
64852 KB |
Output is correct |
25 |
Correct |
30 ms |
64792 KB |
Output is correct |
26 |
Correct |
32 ms |
64804 KB |
Output is correct |
27 |
Correct |
32 ms |
64852 KB |
Output is correct |
28 |
Correct |
30 ms |
64768 KB |
Output is correct |
29 |
Correct |
31 ms |
64724 KB |
Output is correct |
30 |
Correct |
29 ms |
64852 KB |
Output is correct |
31 |
Correct |
39 ms |
64764 KB |
Output is correct |
32 |
Correct |
32 ms |
64756 KB |
Output is correct |
33 |
Correct |
32 ms |
64832 KB |
Output is correct |
34 |
Correct |
33 ms |
64764 KB |
Output is correct |
35 |
Correct |
31 ms |
64764 KB |
Output is correct |
36 |
Correct |
841 ms |
75284 KB |
Output is correct |
37 |
Correct |
388 ms |
70056 KB |
Output is correct |
38 |
Correct |
134 ms |
67424 KB |
Output is correct |
39 |
Correct |
301 ms |
70108 KB |
Output is correct |
40 |
Correct |
528 ms |
75168 KB |
Output is correct |
41 |
Correct |
151 ms |
67460 KB |
Output is correct |
42 |
Correct |
63 ms |
66224 KB |
Output is correct |
43 |
Correct |
130 ms |
67468 KB |
Output is correct |
44 |
Correct |
43 ms |
65324 KB |
Output is correct |
45 |
Correct |
158 ms |
67440 KB |
Output is correct |
46 |
Correct |
58 ms |
66208 KB |
Output is correct |
47 |
Correct |
125 ms |
67420 KB |
Output is correct |
48 |
Correct |
62 ms |
66208 KB |
Output is correct |
49 |
Correct |
119 ms |
67508 KB |
Output is correct |
50 |
Correct |
58 ms |
65540 KB |
Output is correct |
51 |
Correct |
116 ms |
66244 KB |
Output is correct |
52 |
Correct |
41 ms |
65540 KB |
Output is correct |
53 |
Correct |
49 ms |
65288 KB |
Output is correct |
54 |
Correct |
45 ms |
65492 KB |
Output is correct |
55 |
Correct |
55 ms |
65500 KB |
Output is correct |
56 |
Correct |
57 ms |
65524 KB |
Output is correct |
57 |
Correct |
56 ms |
65556 KB |
Output is correct |
58 |
Correct |
49 ms |
65724 KB |
Output is correct |
59 |
Correct |
1457 ms |
85596 KB |
Output is correct |
60 |
Correct |
38 ms |
65196 KB |
Output is correct |
61 |
Correct |
1421 ms |
85404 KB |
Output is correct |
62 |
Correct |
37 ms |
65076 KB |
Output is correct |
63 |
Correct |
1437 ms |
85476 KB |
Output is correct |
64 |
Correct |
38 ms |
65320 KB |
Output is correct |
65 |
Correct |
64 ms |
65588 KB |
Output is correct |
66 |
Correct |
77 ms |
65508 KB |
Output is correct |
67 |
Correct |
56 ms |
65224 KB |
Output is correct |
68 |
Correct |
65 ms |
65200 KB |
Output is correct |
69 |
Correct |
67 ms |
65532 KB |
Output is correct |
70 |
Correct |
118 ms |
66244 KB |
Output is correct |
71 |
Correct |
34 ms |
64824 KB |
Output is correct |
72 |
Correct |
113 ms |
65556 KB |
Output is correct |
73 |
Correct |
32 ms |
64808 KB |
Output is correct |
74 |
Correct |
102 ms |
66256 KB |
Output is correct |
75 |
Correct |
34 ms |
64848 KB |
Output is correct |
76 |
Correct |
371 ms |
67476 KB |
Output is correct |
77 |
Correct |
38 ms |
64844 KB |
Output is correct |
78 |
Correct |
250 ms |
66180 KB |
Output is correct |
79 |
Correct |
49 ms |
65296 KB |
Output is correct |
80 |
Correct |
356 ms |
67520 KB |
Output is correct |
81 |
Correct |
35 ms |
64852 KB |
Output is correct |
82 |
Correct |
183 ms |
67536 KB |
Output is correct |
83 |
Correct |
46 ms |
65072 KB |
Output is correct |
84 |
Correct |
198 ms |
67580 KB |
Output is correct |
85 |
Correct |
31 ms |
64740 KB |
Output is correct |
86 |
Correct |
232 ms |
67504 KB |
Output is correct |
87 |
Correct |
52 ms |
65204 KB |
Output is correct |
88 |
Correct |
89 ms |
65544 KB |
Output is correct |
89 |
Correct |
41 ms |
65036 KB |
Output is correct |
90 |
Correct |
36 ms |
64972 KB |
Output is correct |
91 |
Correct |
92 ms |
65548 KB |
Output is correct |
92 |
Correct |
46 ms |
65072 KB |
Output is correct |
93 |
Correct |
35 ms |
64968 KB |
Output is correct |