This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 200+100, M = 1e5+10, K = 52, MX = 30;
int n;
ll L, dp[N][N][N][2];
array<ll, 2> a[N];
void solve(){
cin >> n >> L;
for(int i = 1; i <= n; ++i) cin >> a[i][0];
for(int i = 1; i <= n; ++i) cin >> a[i][1];
sort(a+1, a+1+n);
for(int i = 0; i <= n; ++i) for(int j = 0; j <= n; ++j) for(int l = 0; l <= n; ++l) dp[i][j][l][0] = dp[i][j][l][1] = (ll)1e18;
a[0] = {0, 0};
a[n + 1] = {L, 0};
dp[0][0][0][0] = dp[0][0][0][1] = 0;
for(int k = 0; k <= n; ++k){
for(int i = 0; i <= n; ++i){
for(int j = 0; j <= n; ++j){
if(i+j < k) continue;
if(i==0&&j==0) continue;
if(i == 0){
dp[i][j][k][1] = min(dp[i][j - 1][k][0] + abs(a[i][0] - a[n - j + 1][0]), dp[i][j - 1][k][1] + abs(a[n - j + 1][0] - a[n - j + 2][0]));
if(k > 0){
if(min(dp[i][j - 1][k - 1][0] + abs(a[i][0] + L - a[n - j + 1][0]), dp[i][j - 1][k - 1][1] + abs(a[n - j + 1][0]- a[n - j + 2][0])) <= a[n - j + 1][1]){
dp[i][j][k][1] = min(dp[i][j][k][1], min(dp[i][j - 1][k - 1][0] + abs(a[i][0] + L - a[n - j + 1][0]), dp[i][j - 1][k - 1][1] + abs(a[n - j + 1][0]- a[n - j + 2][0])));
}
}
}else if(j == 0){
dp[i][j][k][0] = min(dp[i - 1][j][k][0] + abs(a[i][0] - a[i - 1][0]), dp[i - 1][j][k][1] + abs(a[i][0] - a[n - j + 1][0]));
if(k > 0){
if(min(dp[i - 1][j][k - 1][0] + abs(a[i][0] - a[i - 1][0]), dp[i - 1][j][k - 1][1] + abs(a[i][0] + L - a[n - j + 1][0])) <= a[i][1]){
dp[i][j][k][0] = min(dp[i][j][k][1], min(dp[i - 1][j][k - 1][0] + abs(a[i][0] - a[i - 1][0]), dp[i - 1][j][k - 1][1] + abs(a[i][0] + L - a[n - j + 1][0])));
}
}
}else{
dp[i][j][k][0] = min(dp[i - 1][j][k][0] + abs(a[i][0] - a[i - 1][0]), dp[i - 1][j][k][1] + abs(a[i][0] - a[n - j + 1][0]));
dp[i][j][k][1] = min(dp[i][j - 1][k][0] + abs(a[i][0] + L - a[n - j + 1][0]), dp[i][j - 1][k][1] + abs(a[n - j + 1][0] - a[n - j + 2][0]));
if(k > 0){
if(min(dp[i - 1][j][k - 1][0] + abs(a[i][0] - a[i - 1][0]), dp[i - 1][j][k - 1][1] + abs(a[i][0] + L - a[n - j + 1][0])) <= a[i][1]){
dp[i][j][k][0] = min(dp[i][j][k][1], min(dp[i - 1][j][k - 1][0] + abs(a[i][0] - a[i - 1][0]), dp[i - 1][j][k - 1][1] + abs(a[i][0] + L - a[n - j + 1][0])));
}
if(min(dp[i][j - 1][k - 1][0] + abs(a[i][0] + L - a[n - j + 1][0]), dp[i][j - 1][k - 1][1] + abs(a[n - j + 1][0]- a[n - j + 2][0])) <= a[n - j + 1][1]){
dp[i][j][k][1] = min(dp[i][j][k][1], min(dp[i][j - 1][k - 1][0] + abs(a[i][0] + L - a[n - j + 1][0]), dp[i][j - 1][k - 1][1] + abs(a[n - j + 1][0] - a[n - j + 2][0])));
}
}
}
}
}
}
int ans = 0;
for(int i = 0; i <= n; ++i){
for(int k = 0; k <= n; ++k){
if(dp[i][n-i][k][0] < 1e18 || dp[i][n-i][k][1] < 1e18) ans=max(ans,k);
}
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;
while(tt--){
solve();
// en;
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
ho_t3.cpp: In function 'int main()':
ho_t3.cpp:79:15: warning: unused variable 'aa' [-Wunused-variable]
79 | int tt = 1, aa;
| ^~
# | 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... |