/* 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
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 |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
18968 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
18720 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
3 ms |
18944 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
18968 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
18720 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
3 ms |
18944 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
3 ms |
20824 KB |
Output is correct |
19 |
Incorrect |
3 ms |
14800 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
18968 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
18720 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
3 ms |
18944 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Incorrect |
113 ms |
185008 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
18968 KB |
Output is correct |
7 |
Correct |
3 ms |
12632 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
18720 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
3 ms |
18944 KB |
Output is correct |
13 |
Correct |
4 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
18776 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
3 ms |
20824 KB |
Output is correct |
19 |
Incorrect |
3 ms |
14800 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |