#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 |
1 |
Correct |
74 ms |
139092 KB |
Output is correct |
2 |
Correct |
19 ms |
139096 KB |
Output is correct |
3 |
Correct |
18 ms |
139260 KB |
Output is correct |
4 |
Correct |
18 ms |
139100 KB |
Output is correct |
5 |
Correct |
19 ms |
139152 KB |
Output is correct |
6 |
Correct |
18 ms |
139092 KB |
Output is correct |
7 |
Correct |
21 ms |
139124 KB |
Output is correct |
8 |
Correct |
20 ms |
139356 KB |
Output is correct |
9 |
Correct |
21 ms |
139088 KB |
Output is correct |
10 |
Correct |
19 ms |
139100 KB |
Output is correct |
11 |
Correct |
18 ms |
139100 KB |
Output is correct |
12 |
Correct |
18 ms |
139100 KB |
Output is correct |
13 |
Correct |
20 ms |
139224 KB |
Output is correct |
14 |
Correct |
18 ms |
139096 KB |
Output is correct |
15 |
Correct |
18 ms |
139100 KB |
Output is correct |
16 |
Correct |
19 ms |
139100 KB |
Output is correct |
17 |
Correct |
18 ms |
139096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
139092 KB |
Output is correct |
2 |
Correct |
19 ms |
139096 KB |
Output is correct |
3 |
Correct |
18 ms |
139260 KB |
Output is correct |
4 |
Correct |
18 ms |
139100 KB |
Output is correct |
5 |
Correct |
19 ms |
139152 KB |
Output is correct |
6 |
Correct |
18 ms |
139092 KB |
Output is correct |
7 |
Correct |
21 ms |
139124 KB |
Output is correct |
8 |
Correct |
20 ms |
139356 KB |
Output is correct |
9 |
Correct |
21 ms |
139088 KB |
Output is correct |
10 |
Correct |
19 ms |
139100 KB |
Output is correct |
11 |
Correct |
18 ms |
139100 KB |
Output is correct |
12 |
Correct |
18 ms |
139100 KB |
Output is correct |
13 |
Correct |
20 ms |
139224 KB |
Output is correct |
14 |
Correct |
18 ms |
139096 KB |
Output is correct |
15 |
Correct |
18 ms |
139100 KB |
Output is correct |
16 |
Correct |
19 ms |
139100 KB |
Output is correct |
17 |
Correct |
18 ms |
139096 KB |
Output is correct |
18 |
Correct |
18 ms |
139096 KB |
Output is correct |
19 |
Correct |
19 ms |
139100 KB |
Output is correct |
20 |
Correct |
18 ms |
139264 KB |
Output is correct |
21 |
Correct |
19 ms |
139100 KB |
Output is correct |
22 |
Correct |
18 ms |
139100 KB |
Output is correct |
23 |
Correct |
18 ms |
139100 KB |
Output is correct |
24 |
Correct |
18 ms |
139088 KB |
Output is correct |
25 |
Correct |
19 ms |
139100 KB |
Output is correct |
26 |
Correct |
19 ms |
139368 KB |
Output is correct |
27 |
Correct |
18 ms |
139100 KB |
Output is correct |
28 |
Correct |
18 ms |
139296 KB |
Output is correct |
29 |
Correct |
19 ms |
139112 KB |
Output is correct |
30 |
Correct |
18 ms |
139352 KB |
Output is correct |
31 |
Correct |
18 ms |
139096 KB |
Output is correct |
32 |
Correct |
19 ms |
139096 KB |
Output is correct |
33 |
Correct |
19 ms |
139100 KB |
Output is correct |
34 |
Correct |
19 ms |
139156 KB |
Output is correct |
35 |
Correct |
18 ms |
139096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
139092 KB |
Output is correct |
2 |
Correct |
19 ms |
139096 KB |
Output is correct |
3 |
Correct |
18 ms |
139260 KB |
Output is correct |
4 |
Correct |
18 ms |
139100 KB |
Output is correct |
5 |
Correct |
19 ms |
139152 KB |
Output is correct |
6 |
Correct |
18 ms |
139092 KB |
Output is correct |
7 |
Correct |
21 ms |
139124 KB |
Output is correct |
8 |
Correct |
20 ms |
139356 KB |
Output is correct |
9 |
Correct |
21 ms |
139088 KB |
Output is correct |
10 |
Correct |
19 ms |
139100 KB |
Output is correct |
11 |
Correct |
18 ms |
139100 KB |
Output is correct |
12 |
Correct |
18 ms |
139100 KB |
Output is correct |
13 |
Correct |
20 ms |
139224 KB |
Output is correct |
14 |
Correct |
18 ms |
139096 KB |
Output is correct |
15 |
Correct |
18 ms |
139100 KB |
Output is correct |
16 |
Correct |
19 ms |
139100 KB |
Output is correct |
17 |
Correct |
18 ms |
139096 KB |
Output is correct |
18 |
Correct |
41 ms |
139100 KB |
Output is correct |
19 |
Correct |
29 ms |
139320 KB |
Output is correct |
20 |
Correct |
27 ms |
139384 KB |
Output is correct |
21 |
Correct |
31 ms |
139096 KB |
Output is correct |
22 |
Correct |
33 ms |
139188 KB |
Output is correct |
23 |
Correct |
23 ms |
139100 KB |
Output is correct |
24 |
Correct |
22 ms |
139108 KB |
Output is correct |
25 |
Correct |
23 ms |
139352 KB |
Output is correct |
26 |
Correct |
19 ms |
139100 KB |
Output is correct |
27 |
Correct |
24 ms |
139100 KB |
Output is correct |
28 |
Correct |
22 ms |
139100 KB |
Output is correct |
29 |
Correct |
23 ms |
139100 KB |
Output is correct |
30 |
Correct |
22 ms |
139100 KB |
Output is correct |
31 |
Correct |
25 ms |
139096 KB |
Output is correct |
32 |
Correct |
20 ms |
139100 KB |
Output is correct |
33 |
Correct |
23 ms |
139100 KB |
Output is correct |
34 |
Correct |
20 ms |
139092 KB |
Output is correct |
35 |
Correct |
22 ms |
139100 KB |
Output is correct |
36 |
Correct |
19 ms |
139096 KB |
Output is correct |
37 |
Correct |
24 ms |
139104 KB |
Output is correct |
38 |
Correct |
20 ms |
139100 KB |
Output is correct |
39 |
Correct |
24 ms |
139100 KB |
Output is correct |
40 |
Correct |
21 ms |
139100 KB |
Output is correct |
41 |
Correct |
46 ms |
139100 KB |
Output is correct |
42 |
Correct |
32 ms |
139100 KB |
Output is correct |
43 |
Correct |
46 ms |
139304 KB |
Output is correct |
44 |
Correct |
33 ms |
139100 KB |
Output is correct |
45 |
Correct |
50 ms |
139272 KB |
Output is correct |
46 |
Correct |
33 ms |
139100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
139092 KB |
Output is correct |
2 |
Correct |
19 ms |
139096 KB |
Output is correct |
3 |
Correct |
18 ms |
139260 KB |
Output is correct |
4 |
Correct |
18 ms |
139100 KB |
Output is correct |
5 |
Correct |
19 ms |
139152 KB |
Output is correct |
6 |
Correct |
18 ms |
139092 KB |
Output is correct |
7 |
Correct |
21 ms |
139124 KB |
Output is correct |
8 |
Correct |
20 ms |
139356 KB |
Output is correct |
9 |
Correct |
21 ms |
139088 KB |
Output is correct |
10 |
Correct |
19 ms |
139100 KB |
Output is correct |
11 |
Correct |
18 ms |
139100 KB |
Output is correct |
12 |
Correct |
18 ms |
139100 KB |
Output is correct |
13 |
Correct |
20 ms |
139224 KB |
Output is correct |
14 |
Correct |
18 ms |
139096 KB |
Output is correct |
15 |
Correct |
18 ms |
139100 KB |
Output is correct |
16 |
Correct |
19 ms |
139100 KB |
Output is correct |
17 |
Correct |
18 ms |
139096 KB |
Output is correct |
18 |
Correct |
18 ms |
139096 KB |
Output is correct |
19 |
Correct |
19 ms |
139100 KB |
Output is correct |
20 |
Correct |
18 ms |
139264 KB |
Output is correct |
21 |
Correct |
19 ms |
139100 KB |
Output is correct |
22 |
Correct |
18 ms |
139100 KB |
Output is correct |
23 |
Correct |
18 ms |
139100 KB |
Output is correct |
24 |
Correct |
18 ms |
139088 KB |
Output is correct |
25 |
Correct |
19 ms |
139100 KB |
Output is correct |
26 |
Correct |
19 ms |
139368 KB |
Output is correct |
27 |
Correct |
18 ms |
139100 KB |
Output is correct |
28 |
Correct |
18 ms |
139296 KB |
Output is correct |
29 |
Correct |
19 ms |
139112 KB |
Output is correct |
30 |
Correct |
18 ms |
139352 KB |
Output is correct |
31 |
Correct |
18 ms |
139096 KB |
Output is correct |
32 |
Correct |
19 ms |
139096 KB |
Output is correct |
33 |
Correct |
19 ms |
139100 KB |
Output is correct |
34 |
Correct |
19 ms |
139156 KB |
Output is correct |
35 |
Correct |
18 ms |
139096 KB |
Output is correct |
36 |
Correct |
41 ms |
139100 KB |
Output is correct |
37 |
Correct |
29 ms |
139320 KB |
Output is correct |
38 |
Correct |
27 ms |
139384 KB |
Output is correct |
39 |
Correct |
31 ms |
139096 KB |
Output is correct |
40 |
Correct |
33 ms |
139188 KB |
Output is correct |
41 |
Correct |
23 ms |
139100 KB |
Output is correct |
42 |
Correct |
22 ms |
139108 KB |
Output is correct |
43 |
Correct |
23 ms |
139352 KB |
Output is correct |
44 |
Correct |
19 ms |
139100 KB |
Output is correct |
45 |
Correct |
24 ms |
139100 KB |
Output is correct |
46 |
Correct |
22 ms |
139100 KB |
Output is correct |
47 |
Correct |
23 ms |
139100 KB |
Output is correct |
48 |
Correct |
22 ms |
139100 KB |
Output is correct |
49 |
Correct |
25 ms |
139096 KB |
Output is correct |
50 |
Correct |
20 ms |
139100 KB |
Output is correct |
51 |
Correct |
23 ms |
139100 KB |
Output is correct |
52 |
Correct |
20 ms |
139092 KB |
Output is correct |
53 |
Correct |
22 ms |
139100 KB |
Output is correct |
54 |
Correct |
19 ms |
139096 KB |
Output is correct |
55 |
Correct |
24 ms |
139104 KB |
Output is correct |
56 |
Correct |
20 ms |
139100 KB |
Output is correct |
57 |
Correct |
24 ms |
139100 KB |
Output is correct |
58 |
Correct |
21 ms |
139100 KB |
Output is correct |
59 |
Correct |
46 ms |
139100 KB |
Output is correct |
60 |
Correct |
32 ms |
139100 KB |
Output is correct |
61 |
Correct |
46 ms |
139304 KB |
Output is correct |
62 |
Correct |
33 ms |
139100 KB |
Output is correct |
63 |
Correct |
50 ms |
139272 KB |
Output is correct |
64 |
Correct |
33 ms |
139100 KB |
Output is correct |
65 |
Correct |
41 ms |
139100 KB |
Output is correct |
66 |
Correct |
39 ms |
139100 KB |
Output is correct |
67 |
Correct |
37 ms |
139144 KB |
Output is correct |
68 |
Correct |
37 ms |
139100 KB |
Output is correct |
69 |
Correct |
41 ms |
139100 KB |
Output is correct |
70 |
Correct |
42 ms |
139088 KB |
Output is correct |
71 |
Correct |
42 ms |
139100 KB |
Output is correct |
72 |
Correct |
41 ms |
139100 KB |
Output is correct |
73 |
Correct |
37 ms |
139096 KB |
Output is correct |
74 |
Correct |
38 ms |
139100 KB |
Output is correct |
75 |
Correct |
40 ms |
139316 KB |
Output is correct |
76 |
Correct |
44 ms |
139272 KB |
Output is correct |
77 |
Correct |
42 ms |
139100 KB |
Output is correct |
78 |
Correct |
36 ms |
139088 KB |
Output is correct |
79 |
Correct |
36 ms |
139096 KB |
Output is correct |
80 |
Correct |
43 ms |
139100 KB |
Output is correct |
81 |
Correct |
36 ms |
139108 KB |
Output is correct |
82 |
Correct |
40 ms |
139100 KB |
Output is correct |
83 |
Correct |
43 ms |
139308 KB |
Output is correct |
84 |
Correct |
40 ms |
139100 KB |
Output is correct |
85 |
Correct |
41 ms |
139100 KB |
Output is correct |
86 |
Correct |
47 ms |
139216 KB |
Output is correct |
87 |
Correct |
43 ms |
139092 KB |
Output is correct |
88 |
Correct |
46 ms |
139096 KB |
Output is correct |
89 |
Correct |
45 ms |
139100 KB |
Output is correct |
90 |
Correct |
38 ms |
139100 KB |
Output is correct |
91 |
Correct |
46 ms |
139100 KB |
Output is correct |
92 |
Correct |
50 ms |
139332 KB |
Output is correct |
93 |
Correct |
43 ms |
139096 KB |
Output is correct |