/* 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] + 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][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] + L - 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][0],
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] + L - 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][0], 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:91:15: warning: unused variable 'aa' [-Wunused-variable]
91 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
18776 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12748 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
18776 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12748 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
3 ms |
20828 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
20824 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
22996 KB |
Output is correct |
24 |
Correct |
3 ms |
18780 KB |
Output is correct |
25 |
Correct |
4 ms |
20864 KB |
Output is correct |
26 |
Correct |
3 ms |
20828 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
4 ms |
22872 KB |
Output is correct |
31 |
Correct |
4 ms |
21080 KB |
Output is correct |
32 |
Correct |
3 ms |
18780 KB |
Output is correct |
33 |
Correct |
4 ms |
22876 KB |
Output is correct |
34 |
Correct |
4 ms |
23084 KB |
Output is correct |
35 |
Correct |
4 ms |
23128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
18776 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12748 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
113 ms |
183376 KB |
Output is correct |
19 |
Correct |
58 ms |
137552 KB |
Output is correct |
20 |
Correct |
31 ms |
104784 KB |
Output is correct |
21 |
Correct |
52 ms |
134492 KB |
Output is correct |
22 |
Correct |
74 ms |
153940 KB |
Output is correct |
23 |
Correct |
26 ms |
102488 KB |
Output is correct |
24 |
Correct |
20 ms |
96600 KB |
Output is correct |
25 |
Correct |
26 ms |
101976 KB |
Output is correct |
26 |
Correct |
13 ms |
80472 KB |
Output is correct |
27 |
Correct |
26 ms |
100476 KB |
Output is correct |
28 |
Correct |
18 ms |
92108 KB |
Output is correct |
29 |
Correct |
26 ms |
102480 KB |
Output is correct |
30 |
Correct |
21 ms |
94684 KB |
Output is correct |
31 |
Correct |
25 ms |
99164 KB |
Output is correct |
32 |
Correct |
16 ms |
86876 KB |
Output is correct |
33 |
Correct |
30 ms |
99188 KB |
Output is correct |
34 |
Correct |
13 ms |
78424 KB |
Output is correct |
35 |
Correct |
25 ms |
98652 KB |
Output is correct |
36 |
Correct |
15 ms |
85592 KB |
Output is correct |
37 |
Correct |
29 ms |
100308 KB |
Output is correct |
38 |
Correct |
17 ms |
87900 KB |
Output is correct |
39 |
Correct |
27 ms |
101724 KB |
Output is correct |
40 |
Correct |
18 ms |
89436 KB |
Output is correct |
41 |
Correct |
152 ms |
216756 KB |
Output is correct |
42 |
Correct |
88 ms |
165164 KB |
Output is correct |
43 |
Correct |
149 ms |
216732 KB |
Output is correct |
44 |
Correct |
83 ms |
163920 KB |
Output is correct |
45 |
Correct |
151 ms |
216268 KB |
Output is correct |
46 |
Correct |
89 ms |
165972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
10584 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
18776 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
18780 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
3 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12748 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
3 ms |
20828 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
20824 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
22996 KB |
Output is correct |
24 |
Correct |
3 ms |
18780 KB |
Output is correct |
25 |
Correct |
4 ms |
20864 KB |
Output is correct |
26 |
Correct |
3 ms |
20828 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
4 ms |
22876 KB |
Output is correct |
30 |
Correct |
4 ms |
22872 KB |
Output is correct |
31 |
Correct |
4 ms |
21080 KB |
Output is correct |
32 |
Correct |
3 ms |
18780 KB |
Output is correct |
33 |
Correct |
4 ms |
22876 KB |
Output is correct |
34 |
Correct |
4 ms |
23084 KB |
Output is correct |
35 |
Correct |
4 ms |
23128 KB |
Output is correct |
36 |
Correct |
113 ms |
183376 KB |
Output is correct |
37 |
Correct |
58 ms |
137552 KB |
Output is correct |
38 |
Correct |
31 ms |
104784 KB |
Output is correct |
39 |
Correct |
52 ms |
134492 KB |
Output is correct |
40 |
Correct |
74 ms |
153940 KB |
Output is correct |
41 |
Correct |
26 ms |
102488 KB |
Output is correct |
42 |
Correct |
20 ms |
96600 KB |
Output is correct |
43 |
Correct |
26 ms |
101976 KB |
Output is correct |
44 |
Correct |
13 ms |
80472 KB |
Output is correct |
45 |
Correct |
26 ms |
100476 KB |
Output is correct |
46 |
Correct |
18 ms |
92108 KB |
Output is correct |
47 |
Correct |
26 ms |
102480 KB |
Output is correct |
48 |
Correct |
21 ms |
94684 KB |
Output is correct |
49 |
Correct |
25 ms |
99164 KB |
Output is correct |
50 |
Correct |
16 ms |
86876 KB |
Output is correct |
51 |
Correct |
30 ms |
99188 KB |
Output is correct |
52 |
Correct |
13 ms |
78424 KB |
Output is correct |
53 |
Correct |
25 ms |
98652 KB |
Output is correct |
54 |
Correct |
15 ms |
85592 KB |
Output is correct |
55 |
Correct |
29 ms |
100308 KB |
Output is correct |
56 |
Correct |
17 ms |
87900 KB |
Output is correct |
57 |
Correct |
27 ms |
101724 KB |
Output is correct |
58 |
Correct |
18 ms |
89436 KB |
Output is correct |
59 |
Correct |
152 ms |
216756 KB |
Output is correct |
60 |
Correct |
88 ms |
165164 KB |
Output is correct |
61 |
Correct |
149 ms |
216732 KB |
Output is correct |
62 |
Correct |
83 ms |
163920 KB |
Output is correct |
63 |
Correct |
151 ms |
216268 KB |
Output is correct |
64 |
Correct |
89 ms |
165972 KB |
Output is correct |
65 |
Correct |
128 ms |
201200 KB |
Output is correct |
66 |
Correct |
115 ms |
189268 KB |
Output is correct |
67 |
Correct |
109 ms |
183796 KB |
Output is correct |
68 |
Correct |
98 ms |
174024 KB |
Output is correct |
69 |
Correct |
123 ms |
199508 KB |
Output is correct |
70 |
Correct |
121 ms |
193616 KB |
Output is correct |
71 |
Correct |
118 ms |
195152 KB |
Output is correct |
72 |
Correct |
119 ms |
197968 KB |
Output is correct |
73 |
Correct |
106 ms |
187988 KB |
Output is correct |
74 |
Correct |
100 ms |
178896 KB |
Output is correct |
75 |
Correct |
116 ms |
192336 KB |
Output is correct |
76 |
Correct |
143 ms |
211712 KB |
Output is correct |
77 |
Correct |
138 ms |
211712 KB |
Output is correct |
78 |
Correct |
98 ms |
177108 KB |
Output is correct |
79 |
Correct |
100 ms |
181252 KB |
Output is correct |
80 |
Correct |
137 ms |
208648 KB |
Output is correct |
81 |
Correct |
104 ms |
182864 KB |
Output is correct |
82 |
Correct |
111 ms |
189524 KB |
Output is correct |
83 |
Correct |
144 ms |
217836 KB |
Output is correct |
84 |
Correct |
115 ms |
195152 KB |
Output is correct |
85 |
Correct |
130 ms |
206988 KB |
Output is correct |
86 |
Correct |
126 ms |
203852 KB |
Output is correct |
87 |
Correct |
118 ms |
192340 KB |
Output is correct |
88 |
Correct |
151 ms |
219604 KB |
Output is correct |
89 |
Correct |
145 ms |
219476 KB |
Output is correct |
90 |
Correct |
115 ms |
193692 KB |
Output is correct |
91 |
Correct |
152 ms |
219476 KB |
Output is correct |
92 |
Correct |
147 ms |
219480 KB |
Output is correct |
93 |
Correct |
147 ms |
216176 KB |
Output is correct |