#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double
const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN =205;
int n, m;
int a[MAXN], b[MAXN];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
cin>>n>>m;
for(int i= 1;i<=n;i++){
cin>>a[i];
}
for(int i= 1;i<=n;i++){
cin>>b[i];
}
int dp[n+3][n+3][n+3][2];
for(int i = 0;i<=n+1;i++){
for(int j= n+1;j>=0;j--){
for(int k = 0;k<=n+1;k++){
dp[i][j][k][0] = dp[i][j][k][1] = INF;
}
}
}
// i = ith index from right
// j = jth index from left
// k = number of taken stamps
dp[0][n+1][0][0] = 0;
dp[0][n+1][0][1] = 0;
int ans = 0;
for(int i = 0;i<n;i++){
for(int j= n+1;j>=1;j--){
if(j <= i+1) break;
for(int k = 0;k<=n;k++){
for(int p= 0;p<2;p++){
if(p == 0){
int last;
if(j == n+1) last = m;
else last = a[j];
int dist1 = a[i+1] - a[i];
int dist2 = a[i+1] + m - last;
int t1 = min(dp[i][j][k][0] + dist1, dp[i][j][k][1] + dist2);
int newk = k;
if(t1 <= b[i+1]) newk++;
dp[i+1][j][newk][0] = min(dp[i+1][j][newk][0], t1);
if(dp[i+1][j][newk][0] < INF){
ans = max(ans,newk);
}
} else {
int last;
if(j == n+1) last = m;
else last = a[j];
int dist1 = last - a[j-1];
int dist2 = a[i] + m - a[j-1];
int t2 = min(dp[i][j][k][1] + dist1, dp[i][j][k][0] + dist2);
int newk = k;
if(t2 <= b[j-1]) newk++;
dp[i][j-1][newk][1] = min(dp[i][j-1][newk][1], t2);
if(dp[i][j-1][newk][1] < INF){
ans = max(ans, newk);
}
}
}
}
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
107 ms |
94116 KB |
Output is correct |
19 |
Correct |
53 ms |
44928 KB |
Output is correct |
20 |
Correct |
21 ms |
17772 KB |
Output is correct |
21 |
Correct |
48 ms |
41196 KB |
Output is correct |
22 |
Correct |
72 ms |
60524 KB |
Output is correct |
23 |
Correct |
17 ms |
14060 KB |
Output is correct |
24 |
Correct |
11 ms |
9964 KB |
Output is correct |
25 |
Correct |
16 ms |
13676 KB |
Output is correct |
26 |
Correct |
5 ms |
3436 KB |
Output is correct |
27 |
Correct |
17 ms |
14464 KB |
Output is correct |
28 |
Correct |
10 ms |
8812 KB |
Output is correct |
29 |
Correct |
19 ms |
14956 KB |
Output is correct |
30 |
Correct |
12 ms |
10476 KB |
Output is correct |
31 |
Correct |
16 ms |
13676 KB |
Output is correct |
32 |
Correct |
7 ms |
5356 KB |
Output is correct |
33 |
Correct |
19 ms |
13580 KB |
Output is correct |
34 |
Correct |
4 ms |
3180 KB |
Output is correct |
35 |
Correct |
16 ms |
13164 KB |
Output is correct |
36 |
Correct |
5 ms |
4588 KB |
Output is correct |
37 |
Correct |
17 ms |
14444 KB |
Output is correct |
38 |
Correct |
7 ms |
6124 KB |
Output is correct |
39 |
Correct |
18 ms |
15340 KB |
Output is correct |
40 |
Correct |
7 ms |
7148 KB |
Output is correct |
41 |
Correct |
146 ms |
128876 KB |
Output is correct |
42 |
Correct |
83 ms |
71532 KB |
Output is correct |
43 |
Correct |
146 ms |
128796 KB |
Output is correct |
44 |
Correct |
84 ms |
70272 KB |
Output is correct |
45 |
Correct |
131 ms |
128748 KB |
Output is correct |
46 |
Correct |
83 ms |
71660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
107 ms |
94116 KB |
Output is correct |
37 |
Correct |
53 ms |
44928 KB |
Output is correct |
38 |
Correct |
21 ms |
17772 KB |
Output is correct |
39 |
Correct |
48 ms |
41196 KB |
Output is correct |
40 |
Correct |
72 ms |
60524 KB |
Output is correct |
41 |
Correct |
17 ms |
14060 KB |
Output is correct |
42 |
Correct |
11 ms |
9964 KB |
Output is correct |
43 |
Correct |
16 ms |
13676 KB |
Output is correct |
44 |
Correct |
5 ms |
3436 KB |
Output is correct |
45 |
Correct |
17 ms |
14464 KB |
Output is correct |
46 |
Correct |
10 ms |
8812 KB |
Output is correct |
47 |
Correct |
19 ms |
14956 KB |
Output is correct |
48 |
Correct |
12 ms |
10476 KB |
Output is correct |
49 |
Correct |
16 ms |
13676 KB |
Output is correct |
50 |
Correct |
7 ms |
5356 KB |
Output is correct |
51 |
Correct |
19 ms |
13580 KB |
Output is correct |
52 |
Correct |
4 ms |
3180 KB |
Output is correct |
53 |
Correct |
16 ms |
13164 KB |
Output is correct |
54 |
Correct |
5 ms |
4588 KB |
Output is correct |
55 |
Correct |
17 ms |
14444 KB |
Output is correct |
56 |
Correct |
7 ms |
6124 KB |
Output is correct |
57 |
Correct |
18 ms |
15340 KB |
Output is correct |
58 |
Correct |
7 ms |
7148 KB |
Output is correct |
59 |
Correct |
146 ms |
128876 KB |
Output is correct |
60 |
Correct |
83 ms |
71532 KB |
Output is correct |
61 |
Correct |
146 ms |
128796 KB |
Output is correct |
62 |
Correct |
84 ms |
70272 KB |
Output is correct |
63 |
Correct |
131 ms |
128748 KB |
Output is correct |
64 |
Correct |
83 ms |
71660 KB |
Output is correct |
65 |
Correct |
132 ms |
110572 KB |
Output is correct |
66 |
Correct |
117 ms |
97388 KB |
Output is correct |
67 |
Correct |
106 ms |
91264 KB |
Output is correct |
68 |
Correct |
96 ms |
80876 KB |
Output is correct |
69 |
Correct |
129 ms |
108908 KB |
Output is correct |
70 |
Correct |
117 ms |
102124 KB |
Output is correct |
71 |
Correct |
119 ms |
103868 KB |
Output is correct |
72 |
Correct |
122 ms |
105452 KB |
Output is correct |
73 |
Correct |
109 ms |
94316 KB |
Output is correct |
74 |
Correct |
103 ms |
85228 KB |
Output is correct |
75 |
Correct |
113 ms |
98924 KB |
Output is correct |
76 |
Correct |
138 ms |
121196 KB |
Output is correct |
77 |
Correct |
140 ms |
121196 KB |
Output is correct |
78 |
Correct |
96 ms |
82412 KB |
Output is correct |
79 |
Correct |
102 ms |
86636 KB |
Output is correct |
80 |
Correct |
127 ms |
117612 KB |
Output is correct |
81 |
Correct |
101 ms |
88172 KB |
Output is correct |
82 |
Correct |
111 ms |
95724 KB |
Output is correct |
83 |
Correct |
147 ms |
129004 KB |
Output is correct |
84 |
Correct |
123 ms |
102052 KB |
Output is correct |
85 |
Correct |
133 ms |
115948 KB |
Output is correct |
86 |
Correct |
127 ms |
112364 KB |
Output is correct |
87 |
Correct |
113 ms |
98924 KB |
Output is correct |
88 |
Correct |
147 ms |
130668 KB |
Output is correct |
89 |
Correct |
147 ms |
130720 KB |
Output is correct |
90 |
Correct |
114 ms |
100716 KB |
Output is correct |
91 |
Correct |
148 ms |
130668 KB |
Output is correct |
92 |
Correct |
149 ms |
130668 KB |
Output is correct |
93 |
Correct |
144 ms |
126828 KB |
Output is correct |