#include <bits/stdc++.h>
#define TASK "stamp"
#define endl mmm
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define BIT(i,x) (((i)>>(x))&1)
#define FOR(i,a,b) for(int i = (a); i<=(b); i++)
#define FORD(i,a,b) for(int i = (a); i>=(b); i--)
#define all(C) (C).begin(), (C).end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int INT_LIM = 2147483647;
const ll LL_LIM = 9223372036854775807;
template <typename X> bool minimize(X &x, const X &y) {if (x>y){x = y; return true;}return false;}
template <typename X> bool maximize(X &x, const X &y) {if (x<y){x = y; return true;}return false;}
///------------------------------------------///
const int maxn = 205;
int n, L;
int a[maxn];
int b[maxn];
void inp()
{
cin >> n >> L;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n) cin >> b[i];
}
ll f[maxn][maxn][maxn][2];
int ans = 0;
ll dist(int x, int y)
{
if (x>y) swap(x,y);
return min(y-x, L-y+x);
}
void solve()
{
FOR(i, 1, n) FOR(j, 1, n) FOR(c, 0, n) FOR(dir, 0, 1) f[i][j][c][dir] = LL_LIM;
f[1][1][a[1]<=b[1]][0] = f[1][1][a[1]<=b[1]][1] = a[1];
minimize(f[n][n][L-a[n]<=b[n]][0], (ll)L-a[n]);
minimize(f[n][n][L-a[n]<=b[n]][1], (ll)L-a[n]);
FOR(gap, 0, n-1) FOR(l, 1, n)
{
int r = (l+gap-1)%n+1;
FOR(c, 0, n) FOR(dir, 0, 1) if (f[l][r][c][dir]!=LL_LIM)
{
maximize(ans, c);
// cout << l << ' ' << r << ' ' << c << ' ' << dir << ' ' << f[l][r][c][dir] << '\n';
if (gap==n-1) continue;
int s = ((dir==0)?l:r);
int newR = r%n+1;
ll tmp = f[l][r][c][dir]+dist(a[s], a[newR]);
minimize(f[l][newR][c+(tmp<=b[newR])][1], tmp);
int newL = (l-2+n)%n+1;
tmp = f[l][r][c][dir]+dist(a[s], a[newL]);
minimize(f[newL][r][c+(tmp<=b[newL])][0], tmp);
}
}
cout << ans << '\n';
}
signed main()
{
///--------------------------///
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
if (fopen(TASK".INP","r")!=NULL)
{
freopen(TASK".INP","r",stdin);
freopen(TASK".OUT","w",stdout);
}
///--------------------------///
int NTEST = 1;
bool multitest = 0;
if (multitest) cin >> NTEST;
while (NTEST--)
{
inp();
solve();
}
return 0;
}
///------------------------------------------///
컴파일 시 표준 에러 (stderr) 메시지
ho_t3.cpp: In function 'int main()':
ho_t3.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(TASK".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ho_t3.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(TASK".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | 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... |