/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
using namespace std;
const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const int lim = 1e9;
const ll oo = 1e18;
int n, l, ans;
int dp[205][205][205][2];
void solve() {
cin >> n >> l;
int x[n + 1], t[n + 1];
x[0] = t[0] = 0;
for(int i = 1; i <= n; ++i)
cin >> x[i];
for(int i = 1; i <= n; ++i)
cin >> t[i];
auto dis = [&](int idx_1, int idx_2) {
int tmp = abs(x[idx_1] - x[idx_2]);
return min(tmp, l - tmp);
};
auto calc_dis = [&](int idx_1, int idx_2) {
int tmp_1 = l - x[idx_1], tmp_2 = x[idx_2];
int res = tmp_1 + tmp_2 + min(tmp_1, tmp_2);
if(res > lim)
return (ll)-1;
return res;
};
memset(dp, -1, sizeof dp);
dp[0][0][0][0] = dp[0][0][0][2] = 0;
for(int i = n; i > 0; --i) {
for(int j = 0; j < i; ++j) {
int tmp_1 = l - x[i], tmp_2 = x[j];
int res = tmp_1 + tmp_2;
dp[0][i][j][0] = res + tmp_1;
dp[0][i][j][1] = res + tmp_2;
// cout << 0 << " " << i << " " << j << " " << 0 << " " << dp[0][i][j][0] << endl;
// cout << 0 << " " << i << " " << j << " " << 1 << " " << dp[0][i][j][1] << endl;
}
}
// cout << endl;
auto calc_dp = [&](int i, int st, int en, bool point) {
int cur_dp = INT_MAX;
if(point) {
int pre_en = (en + n) % (n + 1);
if(dp[i][st][pre_en][point] != -1)
cur_dp = min(cur_dp, dp[i][st][pre_en][point] + dis(pre_en, en));
if(dp[i][st][pre_en][!point] != -1)
cur_dp = min(cur_dp, dp[i][st][pre_en][!point] + dis(st, en));
if(dp[i - 1][st][pre_en][point] != -1) {
int new_time = dp[i - 1][st][pre_en][point] + dis(pre_en, en);
if(new_time <= t[en])
cur_dp = min(cur_dp, new_time);
}
if(dp[i - 1][st][pre_en][!point] != -1) {
int new_time = dp[i - 1][st][pre_en][!point] + dis(st, en);
if(new_time <= t[en])
cur_dp = min(cur_dp, new_time);
}
if(cur_dp > lim)
cur_dp = -1;
dp[i][st][en][point] = cur_dp;
}
else {
int nxt_st = (st + 1) % (n + 1);
if(dp[i][nxt_st][en][point] != -1)
cur_dp = min(cur_dp, dp[i][nxt_st][en][point] + dis(st, nxt_st));
if(dp[i][nxt_st][en][!point] != -1)
cur_dp = min(cur_dp, dp[i][nxt_st][en][!point] + dis(st, en));
if(dp[i - 1][nxt_st][en][point] != -1) {
int new_time = dp[i - 1][nxt_st][en][point] + dis(st, nxt_st);
if(new_time <= t[st])
cur_dp = min(cur_dp, new_time);
}
if(dp[i - 1][nxt_st][en][!point] != -1) {
int new_time = dp[i - 1][nxt_st][en][!point] + dis(st, en);
if(new_time <= t[st])
cur_dp = min(cur_dp, new_time);
}
if(cur_dp > lim)
cur_dp = -1;
dp[i][st][en][point] = cur_dp;
}
if(cur_dp != -1)
return i;
else
return (ll)-1;
};
for(int i = 1; i <= n; ++i) {
for(int len = i; len <= n; ++len) {
for(int en = 0; en <= len; ++en) {
int st = en - len; if(st < 0) st += n + 1;
for(int point = 0; point <= 1; ++point) {
ans = max(ans, calc_dp(i, st, en, point));
// cout << i << " " << st << " " << en << " " << point << " " << dp[i][st][en][point] << endl;
}
}
}
// cout << endl;
}
cout << ans << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
Compilation message
ho_t3.cpp: In function 'void solve()':
ho_t3.cpp:41:10: warning: variable 'calc_dis' set but not used [-Wunused-but-set-variable]
41 | auto calc_dis = [&](int idx_1, int idx_2) {
| ^~~~~~~~
ho_t3.cpp:50:35: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
50 | dp[0][0][0][0] = dp[0][0][0][2] = 0;
| ~~~~~~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
135172 KB |
Output is correct |
2 |
Correct |
52 ms |
135068 KB |
Output is correct |
3 |
Correct |
53 ms |
135160 KB |
Output is correct |
4 |
Correct |
53 ms |
135172 KB |
Output is correct |
5 |
Correct |
52 ms |
135116 KB |
Output is correct |
6 |
Correct |
55 ms |
135084 KB |
Output is correct |
7 |
Correct |
52 ms |
135116 KB |
Output is correct |
8 |
Correct |
51 ms |
135116 KB |
Output is correct |
9 |
Correct |
52 ms |
135096 KB |
Output is correct |
10 |
Correct |
52 ms |
135120 KB |
Output is correct |
11 |
Correct |
56 ms |
135100 KB |
Output is correct |
12 |
Correct |
53 ms |
135076 KB |
Output is correct |
13 |
Correct |
53 ms |
135084 KB |
Output is correct |
14 |
Correct |
51 ms |
135164 KB |
Output is correct |
15 |
Correct |
51 ms |
135124 KB |
Output is correct |
16 |
Correct |
52 ms |
135068 KB |
Output is correct |
17 |
Correct |
53 ms |
135120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
135172 KB |
Output is correct |
2 |
Correct |
52 ms |
135068 KB |
Output is correct |
3 |
Correct |
53 ms |
135160 KB |
Output is correct |
4 |
Correct |
53 ms |
135172 KB |
Output is correct |
5 |
Correct |
52 ms |
135116 KB |
Output is correct |
6 |
Correct |
55 ms |
135084 KB |
Output is correct |
7 |
Correct |
52 ms |
135116 KB |
Output is correct |
8 |
Correct |
51 ms |
135116 KB |
Output is correct |
9 |
Correct |
52 ms |
135096 KB |
Output is correct |
10 |
Correct |
52 ms |
135120 KB |
Output is correct |
11 |
Correct |
56 ms |
135100 KB |
Output is correct |
12 |
Correct |
53 ms |
135076 KB |
Output is correct |
13 |
Correct |
53 ms |
135084 KB |
Output is correct |
14 |
Correct |
51 ms |
135164 KB |
Output is correct |
15 |
Correct |
51 ms |
135124 KB |
Output is correct |
16 |
Correct |
52 ms |
135068 KB |
Output is correct |
17 |
Correct |
53 ms |
135120 KB |
Output is correct |
18 |
Correct |
55 ms |
135092 KB |
Output is correct |
19 |
Correct |
59 ms |
135080 KB |
Output is correct |
20 |
Correct |
54 ms |
135104 KB |
Output is correct |
21 |
Correct |
53 ms |
135116 KB |
Output is correct |
22 |
Correct |
55 ms |
135116 KB |
Output is correct |
23 |
Correct |
53 ms |
135116 KB |
Output is correct |
24 |
Correct |
60 ms |
135080 KB |
Output is correct |
25 |
Correct |
51 ms |
135112 KB |
Output is correct |
26 |
Correct |
54 ms |
135152 KB |
Output is correct |
27 |
Correct |
53 ms |
135052 KB |
Output is correct |
28 |
Correct |
53 ms |
135128 KB |
Output is correct |
29 |
Correct |
54 ms |
135116 KB |
Output is correct |
30 |
Correct |
53 ms |
135056 KB |
Output is correct |
31 |
Correct |
52 ms |
135076 KB |
Output is correct |
32 |
Correct |
52 ms |
135120 KB |
Output is correct |
33 |
Correct |
52 ms |
135160 KB |
Output is correct |
34 |
Correct |
55 ms |
135084 KB |
Output is correct |
35 |
Correct |
54 ms |
135136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
135172 KB |
Output is correct |
2 |
Correct |
52 ms |
135068 KB |
Output is correct |
3 |
Correct |
53 ms |
135160 KB |
Output is correct |
4 |
Correct |
53 ms |
135172 KB |
Output is correct |
5 |
Correct |
52 ms |
135116 KB |
Output is correct |
6 |
Correct |
55 ms |
135084 KB |
Output is correct |
7 |
Correct |
52 ms |
135116 KB |
Output is correct |
8 |
Correct |
51 ms |
135116 KB |
Output is correct |
9 |
Correct |
52 ms |
135096 KB |
Output is correct |
10 |
Correct |
52 ms |
135120 KB |
Output is correct |
11 |
Correct |
56 ms |
135100 KB |
Output is correct |
12 |
Correct |
53 ms |
135076 KB |
Output is correct |
13 |
Correct |
53 ms |
135084 KB |
Output is correct |
14 |
Correct |
51 ms |
135164 KB |
Output is correct |
15 |
Correct |
51 ms |
135124 KB |
Output is correct |
16 |
Correct |
52 ms |
135068 KB |
Output is correct |
17 |
Correct |
53 ms |
135120 KB |
Output is correct |
18 |
Correct |
107 ms |
135168 KB |
Output is correct |
19 |
Correct |
82 ms |
135172 KB |
Output is correct |
20 |
Correct |
63 ms |
135060 KB |
Output is correct |
21 |
Correct |
79 ms |
135116 KB |
Output is correct |
22 |
Correct |
85 ms |
135156 KB |
Output is correct |
23 |
Correct |
62 ms |
135220 KB |
Output is correct |
24 |
Correct |
57 ms |
135052 KB |
Output is correct |
25 |
Correct |
59 ms |
135104 KB |
Output is correct |
26 |
Incorrect |
55 ms |
135148 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
135172 KB |
Output is correct |
2 |
Correct |
52 ms |
135068 KB |
Output is correct |
3 |
Correct |
53 ms |
135160 KB |
Output is correct |
4 |
Correct |
53 ms |
135172 KB |
Output is correct |
5 |
Correct |
52 ms |
135116 KB |
Output is correct |
6 |
Correct |
55 ms |
135084 KB |
Output is correct |
7 |
Correct |
52 ms |
135116 KB |
Output is correct |
8 |
Correct |
51 ms |
135116 KB |
Output is correct |
9 |
Correct |
52 ms |
135096 KB |
Output is correct |
10 |
Correct |
52 ms |
135120 KB |
Output is correct |
11 |
Correct |
56 ms |
135100 KB |
Output is correct |
12 |
Correct |
53 ms |
135076 KB |
Output is correct |
13 |
Correct |
53 ms |
135084 KB |
Output is correct |
14 |
Correct |
51 ms |
135164 KB |
Output is correct |
15 |
Correct |
51 ms |
135124 KB |
Output is correct |
16 |
Correct |
52 ms |
135068 KB |
Output is correct |
17 |
Correct |
53 ms |
135120 KB |
Output is correct |
18 |
Correct |
55 ms |
135092 KB |
Output is correct |
19 |
Correct |
59 ms |
135080 KB |
Output is correct |
20 |
Correct |
54 ms |
135104 KB |
Output is correct |
21 |
Correct |
53 ms |
135116 KB |
Output is correct |
22 |
Correct |
55 ms |
135116 KB |
Output is correct |
23 |
Correct |
53 ms |
135116 KB |
Output is correct |
24 |
Correct |
60 ms |
135080 KB |
Output is correct |
25 |
Correct |
51 ms |
135112 KB |
Output is correct |
26 |
Correct |
54 ms |
135152 KB |
Output is correct |
27 |
Correct |
53 ms |
135052 KB |
Output is correct |
28 |
Correct |
53 ms |
135128 KB |
Output is correct |
29 |
Correct |
54 ms |
135116 KB |
Output is correct |
30 |
Correct |
53 ms |
135056 KB |
Output is correct |
31 |
Correct |
52 ms |
135076 KB |
Output is correct |
32 |
Correct |
52 ms |
135120 KB |
Output is correct |
33 |
Correct |
52 ms |
135160 KB |
Output is correct |
34 |
Correct |
55 ms |
135084 KB |
Output is correct |
35 |
Correct |
54 ms |
135136 KB |
Output is correct |
36 |
Correct |
107 ms |
135168 KB |
Output is correct |
37 |
Correct |
82 ms |
135172 KB |
Output is correct |
38 |
Correct |
63 ms |
135060 KB |
Output is correct |
39 |
Correct |
79 ms |
135116 KB |
Output is correct |
40 |
Correct |
85 ms |
135156 KB |
Output is correct |
41 |
Correct |
62 ms |
135220 KB |
Output is correct |
42 |
Correct |
57 ms |
135052 KB |
Output is correct |
43 |
Correct |
59 ms |
135104 KB |
Output is correct |
44 |
Incorrect |
55 ms |
135148 KB |
Output isn't correct |
45 |
Halted |
0 ms |
0 KB |
- |