This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define se second
#define fs first
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e2 + 7;
const ll Mod = 1e9 + 7;
const int szBL = 916;
const int INF = 2e9 + 7;
const int BASE = 137;
ll n, P;
ll a[N * 2], T[N * 2];
int dp[N * 2][N][N][2];
void minimize (int &a, ll b){ if (a > b) a = b;}
void solution () {
cin >> n >> P;
rep (i, 1, n) cin >> a[i];
rep (i, 1, n) cin >> T[i];
rep (i, n + 1, n * 2) a[i] = a[i - n], T[i] = T[i - n];
memset(dp, 0x3f, sizeof dp);
dp[n + 1][0][a[n + 1] <= T[n + 1]][1] = dp[n + 1][0][a[n + 1] <= T[n + 1]][0] = a[n + 1];
dp[n][0][P - a[n] <= T[n]][1] = dp[n][0][P - a[n] <= T[n]][0] = P - a[n];
reb (L, n + 1, 1) {
rep (j, 1, n - 1) {
int R = L + j;
if (R < n) continue;
rep (k, 0, n) {
ll costRR = R == n + 1 ? P - a[R - 1] + a[R] : a[R] - a[R - 1];
ll costLL = L == n ? a[L + 1] + P - a[L] : a[L + 1] - a[L];
ll costLR = L == n + 1 ? a[R] - a[L] : P - a[L] + a[R];
ll costRL = R == n ? a[R] - a[L] : P - a[L] + a[R];
minimize (dp[L][j][k][1], costRR + dp[L][j - 1][k][1]);
if (k > 0 && costRR + dp[L][j - 1][k - 1][1] <= T[R]) {
minimize (dp[L][j][k][1], costRR + dp[L][j - 1][k - 1][1]);
}
minimize(dp[L][j][k][1], costLR + dp[L][j - 1][k][0]);
if (k > 0 && costLR + dp[L][j - 1][k - 1][0] <= T[R]) {
minimize (dp[L][j][k][1], costLR + dp[L][j - 1][k - 1][0]);
}
minimize(dp[L][j][k][0], costLL + dp[L + 1][j - 1][k][0]);
if (k > 0 && costLL + dp[L + 1][j - 1][k - 1][0] <= T[L]) {
minimize (dp[L][j][k][0], costLL + dp[L + 1][j - 1][k - 1][0]);
}
minimize (dp[L][j][k][0], costRL + dp[L + 1][j - 1][k][1]);
if (k > 0 && costRL + dp[L + 1][j - 1][k - 1][1] <= T[L]) {
minimize (dp[L][j][k][0], costRL + dp[L + 1][j - 1][k - 1][1]);
}
// rep (p, 0, 1)
// cout << L <<","<<j<<","<<k<<","<<p<<": "<<dp[L][j][k][p]<<" "<<dp[L][j - 1][k - 1][p] + costRR<<"\n";
}
}
}
int res = 0;
rep (i, 1, n + 1)
rep (j, 0, n - 1)
rep (t, 0, n)
rep (k, 0, 1) {
if (dp[i][j][t][k] <= 1e9) res = max (res, t);
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
5 20
4 5 8 13 17
18 23 15 7 10
*/
# | 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... |