#include <bits/stdc++.h>
#define int long long
using namespace std;
using pii = pair<int, int>;
int n, l;
vector<int> x, t;
int dis(int a, int b)
{
assert(0 <= a && 0 <= b && a <= n && b <= n);
return min(min(abs(x[a] - x[b]), x[a] + l - x[b]), l - x[a] + x[b]);
}
void solve()
{
cin >> n >> l;
x.assign(n + 1, 0), t.assign(n + 1, -1);
for (int i = 1; i <= n; ++i)
cin >> x[i];
for (int i = 1; i <= n; ++i)
cin >> t[i];
vector<vector<vector<vector<int>>>> dp(n + 1, vector<vector<vector<int>>>(n + 1, vector<vector<int>>(n + 1, vector<int>(2, LONG_LONG_MAX))));
// priority_queue<array<int, 5>> pq;
// array<int, 5> tmp;
dp[0][0][0][0] = dp[0][0][0][1] = 0;
// for (int i = 0; i <= n; ++i)
// for (int j = 0; j <= n; ++j)
// if (i > j || i == 0)
// {
// dp[0][i][j][0] = dis(0, j) + dis(j, i);
// dp[0][i][j][1] = dis(0, i) + dis(i, j);
// }
// pq.emplace(tmp = {0, 0, 0, 0, 0});
// pq.emplace(tmp = {0, 0, 0, 0, 1});
/*
while (!pq.empty())
{
tmp = pq.top();
pq.pop();
int i = tmp[1], j = tmp[2], k = tmp[3], ll = tmp[4], d = -tmp[0];
if (d != dp[i][j][k][ll])
continue;
// cout << d << ' ';
if (ll == 0)
{
if ((j + n) % (n + 1) != k)
{
if (dp[i][(j + n) % (n + 1)][k][0] > dp[i][j][k][0] + dis(j, (j + n) % (n + 1)))
{
dp[i][(j + n) % (n + 1)][k][0] = dp[i][j][k][0] + dis(j, (j + n) % (n + 1));
pq.emplace(tmp = {-dp[i][(j + n) % (n + 1)][k][0], i, (j + n) % (n + 1), k, 0});
}
if (dp[i][j][k][0] + dis(j, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
{
if (dp[i + 1][(j + n) % (n + 1)][k][0] > dp[i][j][k][0] + dis(j, (j + n) % (n + 1)))
{
dp[i + 1][(j + n) % (n + 1)][k][0] = dp[i][j][k][0] + dis(j, (j + n) % (n + 1));
pq.emplace(tmp = {-dp[i + 1][(j + n) % (n + 1)][k][0], i + 1, (j + n) % (n + 1), k, 0});
}
}
}
if (j != (k + 1) % (n + 1))
{
if (dp[i][j][(k + 1) % (n + 1)][1] > dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)))
{
dp[i][j][(k + 1) % (n + 1)][1] = dp[i][j][k][0] + dis(j, (k + 1) % (n + 1));
pq.emplace(tmp = {-dp[i][j][(k + 1) % (n + 1)][1], i, j, (k + 1) % (n + 1), 1});
}
if (dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
{
if (dp[i + 1][j][(k + 1) % (n + 1)][1] > dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)))
{
dp[i + 1][j][(k + 1) % (n + 1)][1] = dp[i][j][k][0] + dis(j, (k + 1) % (n + 1));
pq.emplace(tmp = {-dp[i + 1][j][(k + 1) % (n + 1)][1], i + 1, j, (k + 1) % (n + 1), 1});
}
}
}
}
else
{
if ((j + n) % (n + 1) != k)
{
if (dp[i][(j + n) % (n + 1)][k][0] > dp[i][j][k][1] + dis(k, (j + n) % (n + 1)))
{
dp[i][(j + n) % (n + 1)][k][0] = dp[i][j][k][1] + dis(k, (j + n) % (n + 1));
pq.emplace(tmp = {-dp[i][(j + n) % (n + 1)][k][0], i, (j + n) % (n + 1), k, 0});
}
if (dp[i][j][k][1] + dis(k, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
{
if (dp[i + 1][(j + n) % (n + 1)][k][0] > dp[i][j][k][1] + dis(k, (j + n) % (n + 1)))
{
dp[i + 1][(j + n) % (n + 1)][k][0] = dp[i][j][k][1] + dis(k, (j + n) % (n + 1));
pq.emplace(tmp = {-dp[i + 1][(j + n) % (n + 1)][k][0], i + 1, (j + n) % (n + 1), k, 0});
}
}
}
if (j != (k + 1) % (n + 1))
{
if (dp[i][j][(k + 1) % (n + 1)][1] > dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)))
{
dp[i][j][(k + 1) % (n + 1)][1] = dp[i][j][k][1] + dis(k, (k + 1) % (n + 1));
pq.emplace(tmp = {-dp[i][j][(k + 1) % (n + 1)][1], i, j, (k + 1) % (n + 1), 1});
}
if (dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
{
if (dp[i + 1][j][(k + 1) % (n + 1)][1] > dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)))
{
dp[i + 1][j][(k + 1) % (n + 1)][1] = dp[i][j][k][1] + dis(k, (k + 1) % (n + 1));
pq.emplace(tmp = {-dp[i + 1][j][(k + 1) % (n + 1)][1], i + 1, j, (k + 1) % (n + 1), 1});
}
}
}
}
}
*/
for (int i = 0; i < n; ++i)
for (int sz = 1; sz <= n; ++sz)
for (int j = 0, k = (j + sz - 1) % (n + 1); j <= n; ++j, k = (k + 1) % (n + 1))
// for (int j = 0; j <= n; ++j)
// for (int k = 0; k <= n; ++k)
{
if (dp[i][j][k][0] != LONG_LONG_MAX)
{
if ((j + n) % (n + 1) != k)
{
dp[i][(j + n) % (n + 1)][k][0] = min(dp[i][(j + n) % (n + 1)][k][0], dp[i][j][k][0] + dis(j, (j + n) % (n + 1)));
if (dp[i][j][k][0] + dis(j, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
dp[i + 1][(j + n) % (n + 1)][k][0] = min(dp[i + 1][(j + n) % (n + 1)][k][0], dp[i][j][k][0] + dis(j, (j + n) % (n + 1)));
}
if (j != (k + 1) % (n + 1))
{
dp[i][j][(k + 1) % (n + 1)][1] = min(dp[i][j][(k + 1) % (n + 1)][1], dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)));
if (dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
dp[i + 1][j][(k + 1) % (n + 1)][1] = min(dp[i + 1][j][(k + 1) % (n + 1)][1], dp[i][j][k][0] + dis(j, (k + 1) % (n + 1)));
}
}
if (dp[i][j][k][1] != LONG_LONG_MAX)
{
if ((j + n) % (n + 1) != k)
{
dp[i][(j + n) % (n + 1)][k][0] = min(dp[i][(j + n) % (n + 1)][k][0], dp[i][j][k][1] + dis(k, (j + n) % (n + 1)));
if (dp[i][j][k][1] + dis(k, (j + n) % (n + 1)) <= t[(j + n) % (n + 1)])
dp[i + 1][(j + n) % (n + 1)][k][0] = min(dp[i + 1][(j + n) % (n + 1)][k][0], dp[i][j][k][1] + dis(k, (j + n) % (n + 1)));
}
if (j != (k + 1) % (n + 1))
{
dp[i][j][(k + 1) % (n + 1)][1] = min(dp[i][j][(k + 1) % (n + 1)][1], dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)));
if (dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)) <= t[(k + 1) % (n + 1)])
dp[i + 1][j][(k + 1) % (n + 1)][1] = min(dp[i + 1][j][(k + 1) % (n + 1)][1], dp[i][j][k][1] + dis(k, (k + 1) % (n + 1)));
}
}
}
int ans = 0;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j)
for (int k = 0; k <= n; ++k)
for (int l = 0; l < 2; ++l)
if (dp[i][j][k][l] != LONG_LONG_MAX)
ans = max(ans, i);
cout << ans << '\n';
}
/*
6 25
3 4 7 17 21 23
11 7 17 10 8 10
5 20
4 5 8 13 17
18 23 15 7 10
4 19
3 7 12 14
2 0 5 4
10 87
9 23 33 38 42 44 45 62 67 78
15 91 7 27 31 53 12 91 89 46
*/
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
408 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
408 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
500 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
408 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
408 ms |
322900 KB |
Output is correct |
19 |
Correct |
230 ms |
152404 KB |
Output is correct |
20 |
Correct |
87 ms |
59480 KB |
Output is correct |
21 |
Correct |
208 ms |
139860 KB |
Output is correct |
22 |
Correct |
265 ms |
206672 KB |
Output is correct |
23 |
Correct |
70 ms |
46680 KB |
Output is correct |
24 |
Correct |
48 ms |
32300 KB |
Output is correct |
25 |
Correct |
67 ms |
45260 KB |
Output is correct |
26 |
Correct |
14 ms |
10840 KB |
Output is correct |
27 |
Correct |
72 ms |
48220 KB |
Output is correct |
28 |
Correct |
41 ms |
29088 KB |
Output is correct |
29 |
Correct |
79 ms |
49756 KB |
Output is correct |
30 |
Correct |
48 ms |
34640 KB |
Output is correct |
31 |
Correct |
68 ms |
45084 KB |
Output is correct |
32 |
Correct |
25 ms |
17240 KB |
Output is correct |
33 |
Correct |
69 ms |
45148 KB |
Output is correct |
34 |
Correct |
11 ms |
9820 KB |
Output is correct |
35 |
Correct |
62 ms |
43796 KB |
Output is correct |
36 |
Correct |
18 ms |
14424 KB |
Output is correct |
37 |
Correct |
67 ms |
48204 KB |
Output is correct |
38 |
Correct |
30 ms |
19548 KB |
Output is correct |
39 |
Correct |
69 ms |
51268 KB |
Output is correct |
40 |
Correct |
32 ms |
23132 KB |
Output is correct |
41 |
Correct |
576 ms |
442536 KB |
Output is correct |
42 |
Correct |
264 ms |
244676 KB |
Output is correct |
43 |
Correct |
547 ms |
442660 KB |
Output is correct |
44 |
Correct |
257 ms |
239820 KB |
Output is correct |
45 |
Correct |
542 ms |
442796 KB |
Output is correct |
46 |
Correct |
261 ms |
244564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
408 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
500 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
408 ms |
322900 KB |
Output is correct |
37 |
Correct |
230 ms |
152404 KB |
Output is correct |
38 |
Correct |
87 ms |
59480 KB |
Output is correct |
39 |
Correct |
208 ms |
139860 KB |
Output is correct |
40 |
Correct |
265 ms |
206672 KB |
Output is correct |
41 |
Correct |
70 ms |
46680 KB |
Output is correct |
42 |
Correct |
48 ms |
32300 KB |
Output is correct |
43 |
Correct |
67 ms |
45260 KB |
Output is correct |
44 |
Correct |
14 ms |
10840 KB |
Output is correct |
45 |
Correct |
72 ms |
48220 KB |
Output is correct |
46 |
Correct |
41 ms |
29088 KB |
Output is correct |
47 |
Correct |
79 ms |
49756 KB |
Output is correct |
48 |
Correct |
48 ms |
34640 KB |
Output is correct |
49 |
Correct |
68 ms |
45084 KB |
Output is correct |
50 |
Correct |
25 ms |
17240 KB |
Output is correct |
51 |
Correct |
69 ms |
45148 KB |
Output is correct |
52 |
Correct |
11 ms |
9820 KB |
Output is correct |
53 |
Correct |
62 ms |
43796 KB |
Output is correct |
54 |
Correct |
18 ms |
14424 KB |
Output is correct |
55 |
Correct |
67 ms |
48204 KB |
Output is correct |
56 |
Correct |
30 ms |
19548 KB |
Output is correct |
57 |
Correct |
69 ms |
51268 KB |
Output is correct |
58 |
Correct |
32 ms |
23132 KB |
Output is correct |
59 |
Correct |
576 ms |
442536 KB |
Output is correct |
60 |
Correct |
264 ms |
244676 KB |
Output is correct |
61 |
Correct |
547 ms |
442660 KB |
Output is correct |
62 |
Correct |
257 ms |
239820 KB |
Output is correct |
63 |
Correct |
542 ms |
442796 KB |
Output is correct |
64 |
Correct |
261 ms |
244564 KB |
Output is correct |
65 |
Correct |
461 ms |
379720 KB |
Output is correct |
66 |
Correct |
407 ms |
333908 KB |
Output is correct |
67 |
Correct |
388 ms |
312724 KB |
Output is correct |
68 |
Correct |
332 ms |
277072 KB |
Output is correct |
69 |
Correct |
447 ms |
373520 KB |
Output is correct |
70 |
Correct |
453 ms |
350292 KB |
Output is correct |
71 |
Correct |
403 ms |
356436 KB |
Output is correct |
72 |
Correct |
472 ms |
361880 KB |
Output is correct |
73 |
Correct |
357 ms |
322900 KB |
Output is correct |
74 |
Correct |
387 ms |
292136 KB |
Output is correct |
75 |
Correct |
381 ms |
339152 KB |
Output is correct |
76 |
Correct |
546 ms |
416652 KB |
Output is correct |
77 |
Correct |
470 ms |
416648 KB |
Output is correct |
78 |
Correct |
380 ms |
282248 KB |
Output is correct |
79 |
Correct |
349 ms |
296760 KB |
Output is correct |
80 |
Correct |
547 ms |
404292 KB |
Output is correct |
81 |
Correct |
347 ms |
301892 KB |
Output is correct |
82 |
Correct |
370 ms |
328336 KB |
Output is correct |
83 |
Correct |
518 ms |
442336 KB |
Output is correct |
84 |
Correct |
403 ms |
350180 KB |
Output is correct |
85 |
Correct |
433 ms |
397652 KB |
Output is correct |
86 |
Correct |
441 ms |
385524 KB |
Output is correct |
87 |
Correct |
426 ms |
339176 KB |
Output is correct |
88 |
Correct |
587 ms |
448904 KB |
Output is correct |
89 |
Correct |
545 ms |
448848 KB |
Output is correct |
90 |
Correct |
365 ms |
345160 KB |
Output is correct |
91 |
Correct |
585 ms |
448900 KB |
Output is correct |
92 |
Correct |
544 ms |
448832 KB |
Output is correct |
93 |
Correct |
457 ms |
435596 KB |
Output is correct |