#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 a = 0; a < n; ++a)
for (int i = 0; i < n; ++i)
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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
756 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
756 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 |
1 ms |
600 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 |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
656 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 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 |
1 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
756 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Execution timed out |
2096 ms |
322908 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
756 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 |
1 ms |
600 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 |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
656 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 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 |
1 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 |
Execution timed out |
2096 ms |
322908 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |