#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define cerr if(false) cerr
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\n'
#define ld long double
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(x) (x).begin(), (x).end()
const int maxn = 250;
int n, l;
int x[maxn], t[maxn];
// [l][r][where][taken] = time
int dp[maxn][maxn][2][maxn];
int mod(int num) {
return (num+n)%n;
}
int dist(int x, int y) {
int res = abs(x-y);
res = min(res, l-res);
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
for(int i = 0; i < maxn; i++) {
for(int j = 0; j < maxn; j++) {
for(int k = 0; k < 2; k++) {
for(int l = 0; l < maxn; l++) {
dp[i][j][k][l] = 1e18;
}
}
}
}
cin >> n >> l;
for(int i = 1; i <= n; i++) {
cin >> x[i];
}
for(int i = 1; i <= n; i++) {
cin >> t[i];
}
dp[0][0][0][0] = dp[0][0][1][0] = 0;
n++;
int ans = 0;
// length of interval including 0 (top of circle)
for(int len = 2; len <= n; len++) {
for(int st = 1-len; st <= 0; st++) {
int l = mod(st);
int r = mod(l+len-1);
for(int i = 0; i <= n; i++) {
// left, right
int l2 = mod(l+1);
int r2 = mod(r-1);
dp[l][r][0][i] = min({dp[l][r][0][i], dp[l2][r][0][i] + dist(x[l], x[l2]), dp[l2][r][1][i] + dist(x[l], x[r])});
dp[l][r][1][i] = min({dp[l][r][1][i], dp[l][r2][0][i] + dist(x[r], x[l]), dp[l][r2][1][i] + dist(x[r], x[r2])});
if(i) {
// TAKE LEFT
// last take left
if(dp[l2][r][0][i-1] + dist(x[l], x[l2]) <= t[l]) {
dp[l][r][0][i] = min(dp[l][r][0][i], dp[l2][r][0][i-1] + dist(x[l], x[l2]));
}
// last take right
if(dp[l2][r][1][i-1] + dist(x[l], x[r]) <= t[l]) {
dp[l][r][0][i] = min(dp[l][r][0][i], dp[l2][r][1][i-1] + dist(x[l], x[r]));
}
// TAKE RIGHT
// last taken left
if(dp[l][r2][0][i-1] + dist(x[l], x[r]) <= t[r]) {
dp[l][r][1][i] = min(dp[l][r][1][i], dp[l][r2][0][i-1] + dist(x[l], x[r]));
}
// last taken right
if(dp[l][r2][1][i-1] + dist(x[r2], x[r]) <= t[r]) {
dp[l][r][1][i] = min(dp[l][r][1][i], dp[l][r2][1][i-1] + dist(x[r2], x[r]));
}
}
if(min(dp[l][r][0][i], dp[l][r][1][i]) != 1e18) {
ans = max(ans, i);
}
}
}
}
cout << ans << endl;
}
/*
[bitmask][current][time]
9 600 000
6 25
3 4 7 17 21 23
11 7 17 10 8 10
-> 4
5 20
4 5 8 13 17
18 23 15 7 10
-> 5
4 19
3 7 12 14
2 0 5 4
-> 4
10 87
9 23 33 38 42 44 45 62 67 78
15 91 7 27 31 53 12 91 89 46
-> 5
Did you read the bounds?
Did you make typos?
Are there edge cases (N=1?)
Are array sizes proper?
Integer overflow?
DS reset properly between test cases?
Is using long longs causing TLE?
Are you using floating points?
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
244972 KB |
Output is correct |
2 |
Correct |
148 ms |
245100 KB |
Output is correct |
3 |
Correct |
145 ms |
244972 KB |
Output is correct |
4 |
Correct |
145 ms |
244972 KB |
Output is correct |
5 |
Correct |
146 ms |
244972 KB |
Output is correct |
6 |
Correct |
142 ms |
245100 KB |
Output is correct |
7 |
Correct |
143 ms |
244972 KB |
Output is correct |
8 |
Correct |
142 ms |
245100 KB |
Output is correct |
9 |
Correct |
141 ms |
244972 KB |
Output is correct |
10 |
Correct |
145 ms |
244972 KB |
Output is correct |
11 |
Correct |
149 ms |
245100 KB |
Output is correct |
12 |
Correct |
142 ms |
244972 KB |
Output is correct |
13 |
Correct |
145 ms |
245100 KB |
Output is correct |
14 |
Correct |
141 ms |
244972 KB |
Output is correct |
15 |
Correct |
152 ms |
244972 KB |
Output is correct |
16 |
Correct |
141 ms |
244972 KB |
Output is correct |
17 |
Correct |
143 ms |
245100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
244972 KB |
Output is correct |
2 |
Correct |
148 ms |
245100 KB |
Output is correct |
3 |
Correct |
145 ms |
244972 KB |
Output is correct |
4 |
Correct |
145 ms |
244972 KB |
Output is correct |
5 |
Correct |
146 ms |
244972 KB |
Output is correct |
6 |
Correct |
142 ms |
245100 KB |
Output is correct |
7 |
Correct |
143 ms |
244972 KB |
Output is correct |
8 |
Correct |
142 ms |
245100 KB |
Output is correct |
9 |
Correct |
141 ms |
244972 KB |
Output is correct |
10 |
Correct |
145 ms |
244972 KB |
Output is correct |
11 |
Correct |
149 ms |
245100 KB |
Output is correct |
12 |
Correct |
142 ms |
244972 KB |
Output is correct |
13 |
Correct |
145 ms |
245100 KB |
Output is correct |
14 |
Correct |
141 ms |
244972 KB |
Output is correct |
15 |
Correct |
152 ms |
244972 KB |
Output is correct |
16 |
Correct |
141 ms |
244972 KB |
Output is correct |
17 |
Correct |
143 ms |
245100 KB |
Output is correct |
18 |
Correct |
142 ms |
244972 KB |
Output is correct |
19 |
Correct |
143 ms |
244972 KB |
Output is correct |
20 |
Correct |
145 ms |
244972 KB |
Output is correct |
21 |
Correct |
145 ms |
244972 KB |
Output is correct |
22 |
Correct |
144 ms |
244972 KB |
Output is correct |
23 |
Correct |
142 ms |
244972 KB |
Output is correct |
24 |
Correct |
143 ms |
244972 KB |
Output is correct |
25 |
Correct |
143 ms |
245100 KB |
Output is correct |
26 |
Correct |
144 ms |
244972 KB |
Output is correct |
27 |
Correct |
156 ms |
244972 KB |
Output is correct |
28 |
Correct |
145 ms |
244972 KB |
Output is correct |
29 |
Correct |
145 ms |
244972 KB |
Output is correct |
30 |
Correct |
143 ms |
244972 KB |
Output is correct |
31 |
Correct |
141 ms |
244972 KB |
Output is correct |
32 |
Correct |
143 ms |
244972 KB |
Output is correct |
33 |
Correct |
151 ms |
244984 KB |
Output is correct |
34 |
Correct |
144 ms |
244972 KB |
Output is correct |
35 |
Correct |
141 ms |
244972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
244972 KB |
Output is correct |
2 |
Correct |
148 ms |
245100 KB |
Output is correct |
3 |
Correct |
145 ms |
244972 KB |
Output is correct |
4 |
Correct |
145 ms |
244972 KB |
Output is correct |
5 |
Correct |
146 ms |
244972 KB |
Output is correct |
6 |
Correct |
142 ms |
245100 KB |
Output is correct |
7 |
Correct |
143 ms |
244972 KB |
Output is correct |
8 |
Correct |
142 ms |
245100 KB |
Output is correct |
9 |
Correct |
141 ms |
244972 KB |
Output is correct |
10 |
Correct |
145 ms |
244972 KB |
Output is correct |
11 |
Correct |
149 ms |
245100 KB |
Output is correct |
12 |
Correct |
142 ms |
244972 KB |
Output is correct |
13 |
Correct |
145 ms |
245100 KB |
Output is correct |
14 |
Correct |
141 ms |
244972 KB |
Output is correct |
15 |
Correct |
152 ms |
244972 KB |
Output is correct |
16 |
Correct |
141 ms |
244972 KB |
Output is correct |
17 |
Correct |
143 ms |
245100 KB |
Output is correct |
18 |
Correct |
171 ms |
244972 KB |
Output is correct |
19 |
Correct |
155 ms |
244972 KB |
Output is correct |
20 |
Correct |
148 ms |
244972 KB |
Output is correct |
21 |
Correct |
157 ms |
245012 KB |
Output is correct |
22 |
Correct |
160 ms |
244972 KB |
Output is correct |
23 |
Correct |
147 ms |
244972 KB |
Output is correct |
24 |
Correct |
148 ms |
244972 KB |
Output is correct |
25 |
Correct |
158 ms |
245000 KB |
Output is correct |
26 |
Correct |
147 ms |
244972 KB |
Output is correct |
27 |
Correct |
149 ms |
244972 KB |
Output is correct |
28 |
Correct |
151 ms |
244972 KB |
Output is correct |
29 |
Correct |
151 ms |
244972 KB |
Output is correct |
30 |
Correct |
151 ms |
244972 KB |
Output is correct |
31 |
Correct |
147 ms |
244972 KB |
Output is correct |
32 |
Correct |
165 ms |
244972 KB |
Output is correct |
33 |
Correct |
149 ms |
244972 KB |
Output is correct |
34 |
Correct |
146 ms |
244972 KB |
Output is correct |
35 |
Correct |
150 ms |
244972 KB |
Output is correct |
36 |
Correct |
155 ms |
244972 KB |
Output is correct |
37 |
Correct |
148 ms |
244972 KB |
Output is correct |
38 |
Correct |
144 ms |
244948 KB |
Output is correct |
39 |
Correct |
150 ms |
244992 KB |
Output is correct |
40 |
Correct |
146 ms |
244920 KB |
Output is correct |
41 |
Correct |
190 ms |
245000 KB |
Output is correct |
42 |
Correct |
172 ms |
244892 KB |
Output is correct |
43 |
Correct |
199 ms |
244972 KB |
Output is correct |
44 |
Correct |
171 ms |
245100 KB |
Output is correct |
45 |
Correct |
188 ms |
244956 KB |
Output is correct |
46 |
Correct |
166 ms |
244972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
244972 KB |
Output is correct |
2 |
Correct |
148 ms |
245100 KB |
Output is correct |
3 |
Correct |
145 ms |
244972 KB |
Output is correct |
4 |
Correct |
145 ms |
244972 KB |
Output is correct |
5 |
Correct |
146 ms |
244972 KB |
Output is correct |
6 |
Correct |
142 ms |
245100 KB |
Output is correct |
7 |
Correct |
143 ms |
244972 KB |
Output is correct |
8 |
Correct |
142 ms |
245100 KB |
Output is correct |
9 |
Correct |
141 ms |
244972 KB |
Output is correct |
10 |
Correct |
145 ms |
244972 KB |
Output is correct |
11 |
Correct |
149 ms |
245100 KB |
Output is correct |
12 |
Correct |
142 ms |
244972 KB |
Output is correct |
13 |
Correct |
145 ms |
245100 KB |
Output is correct |
14 |
Correct |
141 ms |
244972 KB |
Output is correct |
15 |
Correct |
152 ms |
244972 KB |
Output is correct |
16 |
Correct |
141 ms |
244972 KB |
Output is correct |
17 |
Correct |
143 ms |
245100 KB |
Output is correct |
18 |
Correct |
142 ms |
244972 KB |
Output is correct |
19 |
Correct |
143 ms |
244972 KB |
Output is correct |
20 |
Correct |
145 ms |
244972 KB |
Output is correct |
21 |
Correct |
145 ms |
244972 KB |
Output is correct |
22 |
Correct |
144 ms |
244972 KB |
Output is correct |
23 |
Correct |
142 ms |
244972 KB |
Output is correct |
24 |
Correct |
143 ms |
244972 KB |
Output is correct |
25 |
Correct |
143 ms |
245100 KB |
Output is correct |
26 |
Correct |
144 ms |
244972 KB |
Output is correct |
27 |
Correct |
156 ms |
244972 KB |
Output is correct |
28 |
Correct |
145 ms |
244972 KB |
Output is correct |
29 |
Correct |
145 ms |
244972 KB |
Output is correct |
30 |
Correct |
143 ms |
244972 KB |
Output is correct |
31 |
Correct |
141 ms |
244972 KB |
Output is correct |
32 |
Correct |
143 ms |
244972 KB |
Output is correct |
33 |
Correct |
151 ms |
244984 KB |
Output is correct |
34 |
Correct |
144 ms |
244972 KB |
Output is correct |
35 |
Correct |
141 ms |
244972 KB |
Output is correct |
36 |
Correct |
171 ms |
244972 KB |
Output is correct |
37 |
Correct |
155 ms |
244972 KB |
Output is correct |
38 |
Correct |
148 ms |
244972 KB |
Output is correct |
39 |
Correct |
157 ms |
245012 KB |
Output is correct |
40 |
Correct |
160 ms |
244972 KB |
Output is correct |
41 |
Correct |
147 ms |
244972 KB |
Output is correct |
42 |
Correct |
148 ms |
244972 KB |
Output is correct |
43 |
Correct |
158 ms |
245000 KB |
Output is correct |
44 |
Correct |
147 ms |
244972 KB |
Output is correct |
45 |
Correct |
149 ms |
244972 KB |
Output is correct |
46 |
Correct |
151 ms |
244972 KB |
Output is correct |
47 |
Correct |
151 ms |
244972 KB |
Output is correct |
48 |
Correct |
151 ms |
244972 KB |
Output is correct |
49 |
Correct |
147 ms |
244972 KB |
Output is correct |
50 |
Correct |
165 ms |
244972 KB |
Output is correct |
51 |
Correct |
149 ms |
244972 KB |
Output is correct |
52 |
Correct |
146 ms |
244972 KB |
Output is correct |
53 |
Correct |
150 ms |
244972 KB |
Output is correct |
54 |
Correct |
155 ms |
244972 KB |
Output is correct |
55 |
Correct |
148 ms |
244972 KB |
Output is correct |
56 |
Correct |
144 ms |
244948 KB |
Output is correct |
57 |
Correct |
150 ms |
244992 KB |
Output is correct |
58 |
Correct |
146 ms |
244920 KB |
Output is correct |
59 |
Correct |
190 ms |
245000 KB |
Output is correct |
60 |
Correct |
172 ms |
244892 KB |
Output is correct |
61 |
Correct |
199 ms |
244972 KB |
Output is correct |
62 |
Correct |
171 ms |
245100 KB |
Output is correct |
63 |
Correct |
188 ms |
244956 KB |
Output is correct |
64 |
Correct |
166 ms |
244972 KB |
Output is correct |
65 |
Correct |
181 ms |
245100 KB |
Output is correct |
66 |
Correct |
183 ms |
245100 KB |
Output is correct |
67 |
Correct |
175 ms |
245000 KB |
Output is correct |
68 |
Correct |
169 ms |
244960 KB |
Output is correct |
69 |
Correct |
183 ms |
244972 KB |
Output is correct |
70 |
Correct |
183 ms |
245020 KB |
Output is correct |
71 |
Correct |
178 ms |
244964 KB |
Output is correct |
72 |
Correct |
190 ms |
244972 KB |
Output is correct |
73 |
Correct |
178 ms |
244972 KB |
Output is correct |
74 |
Correct |
184 ms |
244972 KB |
Output is correct |
75 |
Correct |
191 ms |
244988 KB |
Output is correct |
76 |
Correct |
183 ms |
244972 KB |
Output is correct |
77 |
Correct |
188 ms |
245092 KB |
Output is correct |
78 |
Correct |
179 ms |
244972 KB |
Output is correct |
79 |
Correct |
177 ms |
244972 KB |
Output is correct |
80 |
Correct |
185 ms |
244972 KB |
Output is correct |
81 |
Correct |
171 ms |
244972 KB |
Output is correct |
82 |
Correct |
172 ms |
244972 KB |
Output is correct |
83 |
Correct |
180 ms |
245100 KB |
Output is correct |
84 |
Correct |
199 ms |
244896 KB |
Output is correct |
85 |
Correct |
192 ms |
244972 KB |
Output is correct |
86 |
Correct |
184 ms |
244908 KB |
Output is correct |
87 |
Correct |
185 ms |
245176 KB |
Output is correct |
88 |
Correct |
201 ms |
244972 KB |
Output is correct |
89 |
Correct |
184 ms |
244972 KB |
Output is correct |
90 |
Correct |
173 ms |
244972 KB |
Output is correct |
91 |
Correct |
205 ms |
244972 KB |
Output is correct |
92 |
Correct |
189 ms |
244972 KB |
Output is correct |
93 |
Correct |
186 ms |
244968 KB |
Output is correct |