#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
long long X[202];
long long T[202];
long long L;
long long dist(int a, int b)
{
return min( abs(X[a] - X[b]), min(X[a], abs(L - X[a])) + min(X[b], abs(L - X[b])) );
}
int main()
{
long long inf = 1;
for(int i = 1; i <= 18; i++) inf = inf * 10LL;
int N;
cin >> N >> L;
for(int i = 1; i <= N; i++) cin >> X[i];
for(int i = 1; i <= N; i++) cin >> T[i];
long long dp_left[N+2][N+2][N+2], dp_right[N+2][N+2][N+2];
for(int i = 0; i <= N+1; i++)
for(int j = 0; j <= N+1; j++)
for(int k = 0; k <= N+1; k++)
dp_left[i][j][k] = dp_right[i][j][k] = inf;
//minimum time needed to collect k stamps from among j, i+1, ... N, 1, .... i and end up at left/right.
X[0] = X[N+1] = 0;
int res = 0;
dp_left[0][N+1][0] = dp_right[0][N+1][0] = 0;
for(int k = 0; k <= N; k++)
{
for(int i = 0; i <= N; i++)
{
for(int j = N+1; j > i; j--)
{
//don't collect
if(j < N+1)
{
dp_left[i][j][k] = min(dp_left[i][j][k], dp_left[i][j+1][k] + dist(j, j+1));
dp_left[i][j][k] = min(dp_left[i][j][k], dp_right[i][j+1][k] + dist(i, j));
}
if(i > 0)
{
dp_right[i][j][k] = min(dp_right[i][j][k], dp_right[i-1][j][k] + dist(i, i-1));
dp_right[i][j][k] = min(dp_right[i][j][k], dp_left[i-1][j][k] + dist(i, j));
}
if(k == 0) continue;
//collect
if(j < N+1)
{
if(dp_left[i][j+1][k-1] + dist(j+1, j) <= T[j])
dp_left[i][j][k] = min(dp_left[i][j][k], dp_left[i][j+1][k-1] + dist(j+1, j));
if(dp_right[i][j+1][k-1] + dist(i, j) <= T[j])
dp_left[i][j][k] = min(dp_left[i][j][k], dp_right[i][j+1][k-1] + dist(i, j));
}
if(i > 0)
{
if(dp_right[i-1][j][k-1] + dist(i-1, i) <= T[i])
dp_right[i][j][k] = min(dp_right[i][j][k], dp_right[i-1][j][k-1] + dist(i-1, i));
if(dp_left[i-1][j][k-1] + dist(i, j) <= T[i])
dp_right[i][j][k] = min(dp_right[i][j][k], dp_left[i-1][j][k-1] + dist(i, j));
}
if(dp_left[i][j][k] != inf) res = max(res, k);
if(dp_right[i][j][k] != inf) res = max(res, k);
// cout << i << ' ' << j << ' ' << k << " -> " << dp_left[i][j][k] << ' ' << dp_right[i][j][k] << '\n';
}
}
}
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 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 |
364 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 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 |
194 ms |
93292 KB |
Output is correct |
19 |
Correct |
95 ms |
44140 KB |
Output is correct |
20 |
Correct |
39 ms |
17388 KB |
Output is correct |
21 |
Correct |
86 ms |
40812 KB |
Output is correct |
22 |
Correct |
135 ms |
59764 KB |
Output is correct |
23 |
Correct |
31 ms |
13676 KB |
Output is correct |
24 |
Correct |
19 ms |
9580 KB |
Output is correct |
25 |
Correct |
29 ms |
13292 KB |
Output is correct |
26 |
Correct |
8 ms |
3308 KB |
Output is correct |
27 |
Correct |
33 ms |
14316 KB |
Output is correct |
28 |
Correct |
18 ms |
8684 KB |
Output is correct |
29 |
Correct |
33 ms |
14700 KB |
Output is correct |
30 |
Correct |
21 ms |
10220 KB |
Output is correct |
31 |
Correct |
29 ms |
13292 KB |
Output is correct |
32 |
Correct |
10 ms |
5228 KB |
Output is correct |
33 |
Correct |
28 ms |
13420 KB |
Output is correct |
34 |
Correct |
6 ms |
3052 KB |
Output is correct |
35 |
Correct |
34 ms |
12908 KB |
Output is correct |
36 |
Correct |
9 ms |
4588 KB |
Output is correct |
37 |
Correct |
29 ms |
14188 KB |
Output is correct |
38 |
Correct |
12 ms |
5868 KB |
Output is correct |
39 |
Correct |
30 ms |
15104 KB |
Output is correct |
40 |
Correct |
14 ms |
6892 KB |
Output is correct |
41 |
Correct |
274 ms |
127596 KB |
Output is correct |
42 |
Correct |
143 ms |
70636 KB |
Output is correct |
43 |
Correct |
284 ms |
127468 KB |
Output is correct |
44 |
Correct |
140 ms |
69424 KB |
Output is correct |
45 |
Correct |
267 ms |
127468 KB |
Output is correct |
46 |
Correct |
145 ms |
70764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 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 |
364 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 |
194 ms |
93292 KB |
Output is correct |
37 |
Correct |
95 ms |
44140 KB |
Output is correct |
38 |
Correct |
39 ms |
17388 KB |
Output is correct |
39 |
Correct |
86 ms |
40812 KB |
Output is correct |
40 |
Correct |
135 ms |
59764 KB |
Output is correct |
41 |
Correct |
31 ms |
13676 KB |
Output is correct |
42 |
Correct |
19 ms |
9580 KB |
Output is correct |
43 |
Correct |
29 ms |
13292 KB |
Output is correct |
44 |
Correct |
8 ms |
3308 KB |
Output is correct |
45 |
Correct |
33 ms |
14316 KB |
Output is correct |
46 |
Correct |
18 ms |
8684 KB |
Output is correct |
47 |
Correct |
33 ms |
14700 KB |
Output is correct |
48 |
Correct |
21 ms |
10220 KB |
Output is correct |
49 |
Correct |
29 ms |
13292 KB |
Output is correct |
50 |
Correct |
10 ms |
5228 KB |
Output is correct |
51 |
Correct |
28 ms |
13420 KB |
Output is correct |
52 |
Correct |
6 ms |
3052 KB |
Output is correct |
53 |
Correct |
34 ms |
12908 KB |
Output is correct |
54 |
Correct |
9 ms |
4588 KB |
Output is correct |
55 |
Correct |
29 ms |
14188 KB |
Output is correct |
56 |
Correct |
12 ms |
5868 KB |
Output is correct |
57 |
Correct |
30 ms |
15104 KB |
Output is correct |
58 |
Correct |
14 ms |
6892 KB |
Output is correct |
59 |
Correct |
274 ms |
127596 KB |
Output is correct |
60 |
Correct |
143 ms |
70636 KB |
Output is correct |
61 |
Correct |
284 ms |
127468 KB |
Output is correct |
62 |
Correct |
140 ms |
69424 KB |
Output is correct |
63 |
Correct |
267 ms |
127468 KB |
Output is correct |
64 |
Correct |
145 ms |
70764 KB |
Output is correct |
65 |
Correct |
238 ms |
109548 KB |
Output is correct |
66 |
Correct |
205 ms |
96312 KB |
Output is correct |
67 |
Correct |
195 ms |
90220 KB |
Output is correct |
68 |
Correct |
191 ms |
80108 KB |
Output is correct |
69 |
Correct |
223 ms |
107756 KB |
Output is correct |
70 |
Correct |
221 ms |
101100 KB |
Output is correct |
71 |
Correct |
209 ms |
102764 KB |
Output is correct |
72 |
Correct |
234 ms |
104300 KB |
Output is correct |
73 |
Correct |
197 ms |
93272 KB |
Output is correct |
74 |
Correct |
191 ms |
84272 KB |
Output is correct |
75 |
Correct |
196 ms |
97784 KB |
Output is correct |
76 |
Correct |
275 ms |
120060 KB |
Output is correct |
77 |
Correct |
249 ms |
120172 KB |
Output is correct |
78 |
Correct |
184 ms |
81516 KB |
Output is correct |
79 |
Correct |
187 ms |
85612 KB |
Output is correct |
80 |
Correct |
268 ms |
116332 KB |
Output is correct |
81 |
Correct |
179 ms |
87148 KB |
Output is correct |
82 |
Correct |
218 ms |
94700 KB |
Output is correct |
83 |
Correct |
265 ms |
127468 KB |
Output is correct |
84 |
Correct |
209 ms |
101228 KB |
Output is correct |
85 |
Correct |
250 ms |
114676 KB |
Output is correct |
86 |
Correct |
253 ms |
111084 KB |
Output is correct |
87 |
Correct |
201 ms |
97900 KB |
Output is correct |
88 |
Correct |
289 ms |
129388 KB |
Output is correct |
89 |
Correct |
277 ms |
129388 KB |
Output is correct |
90 |
Correct |
208 ms |
99564 KB |
Output is correct |
91 |
Correct |
291 ms |
129388 KB |
Output is correct |
92 |
Correct |
285 ms |
129404 KB |
Output is correct |
93 |
Correct |
284 ms |
125804 KB |
Output is correct |