#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 205;
ll dp[MAXN + 1][MAXN + 1][MAXN + 1][2];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
ll Lt;
cin >> N >> Lt;
vector<ll> x(N + 3);
for (int i = 1; i <= N; ++i) {
cin >> x[i];
}
vector<ll> t(N + 3);
for (int i = 1; i <= N; ++i) {
cin >> t[i];
}
for (int i = 0; i <= N + 2; ++i) {
for (int j = 0; j <= N + 2; ++j) {
for (int k = 0; k <= N + 2; ++k) {
for (int l = 0; l < 2; ++l) {
dp[i][j][k][l] = -1;
}
}
}
}
auto dist = [&](int L, int R, int side, int to) -> ll {
ll mn = 1e18;
if (side == 0) {
mn = min(abs(x[L] - x[to]), Lt - abs(x[L] - x[to]));
} else {
mn = min(abs(x[R] - x[to]), Lt - abs(x[R] - x[to]));
}
return mn;
};
auto dfs = [&](auto& dfs, int L, int R, int side, int take, ll T) -> ll {
if (L >= R) return 0;
if (dp[L][R][take][side] != -1) return dp[L][R][take][side];
ll res = 1e18;
if (L + 1 <= R && t[L + 1] >= T + dist(L, R, side, L + 1)) {
res = min(res, dfs(dfs, L + 1, R, 0, take + 1, T + dist(L, R, side, L + 1)));
} else if (L + 1 <= R) {
res = min(res, dfs(dfs, L + 1, R, 0, take, T + dist(L, R, side, L + 1)));
}
if (R - 1 >= L && t[R - 1] >= T + dist(L, R, side, R - 1)) {
res = min(res, dfs(dfs, L, R - 1, 1, take + 1, T + dist(L, R, side, R - 1)));
} else if (R - 1 >= L) {
res = min(res, dfs(dfs, L, R - 1, 1, take, T + dist(L, R, side, R - 1)));
}
return dp[L][R][take][side] = res;
};
dfs(dfs, 0, N + 1, 0, 0, 0);
dfs(dfs, 0, N + 1, 1, 0, 0);
int res = -1;
for (int i = 0; i <= N + 2; ++i) {
for (int j = 0; j <= N + 2; ++j) {
for (int k = 0; k <= N; ++k) {
for (int l = 0; l < 2; ++l) {
if (dp[i][j][k][l] == -1) continue;
if (dp[i][j][k][l] <= 1e18) {
res = max(res, k);
}
}
}
}
}
cout << res << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |