#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
const ll INF = 1e18;
inline void mn(ll &A, ll B) { A = min(A, B); }
ll a[401], t[401], dp[401][401][401][2];
// dp[i][j][k][l] = Min time if visited range = [i, j] with k stamps collected. l = Currently at i
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, p;
cin >> n >> p;
FOR(i, 0, n) {
cin >> a[i];
a[i + n + 1] = a[i];
}
FOR(i, 0, n) {
cin >> t[i];
t[i + n + 1] = t[i];
}
memset(dp, 0x3f, sizeof(dp));
dp[n][n][0][0] = 0;
int ans = 0;
FOR(range, 0, n + 1) FOR(i, n - range, n + 1) FOR(k, 0, range + 1) FOR(l, 0, 2) {
int j = i + range, curr = (l ? i : j);
if (dp[i][j][k][l] > INF) continue;
if (j < 2 * n - 1) {
ll dist = min(p - abs(a[curr] - a[j + 1]), abs(a[curr] - a[j + 1])) + dp[i][j][k][l];
mn(dp[i][j + 1][k + (dist <= t[j + 1])][0], dist);
}
if (i) {
ll dist = min(p - abs(a[curr] - a[i - 1]), abs(a[curr] - a[i - 1])) + dp[i][j][k][l];
mn(dp[i - 1][j][k + (dist <= t[i - 1])][1], dist);
}
ans = max(ans, k);
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
1009876 KB |
Output is correct |
2 |
Correct |
479 ms |
1010108 KB |
Output is correct |
3 |
Correct |
479 ms |
1009784 KB |
Output is correct |
4 |
Correct |
481 ms |
1009784 KB |
Output is correct |
5 |
Correct |
488 ms |
1009784 KB |
Output is correct |
6 |
Correct |
477 ms |
1009812 KB |
Output is correct |
7 |
Correct |
481 ms |
1009892 KB |
Output is correct |
8 |
Correct |
478 ms |
1009784 KB |
Output is correct |
9 |
Correct |
483 ms |
1009864 KB |
Output is correct |
10 |
Correct |
479 ms |
1009784 KB |
Output is correct |
11 |
Correct |
479 ms |
1009784 KB |
Output is correct |
12 |
Correct |
480 ms |
1009912 KB |
Output is correct |
13 |
Correct |
483 ms |
1009784 KB |
Output is correct |
14 |
Correct |
484 ms |
1009992 KB |
Output is correct |
15 |
Correct |
482 ms |
1009788 KB |
Output is correct |
16 |
Correct |
478 ms |
1009784 KB |
Output is correct |
17 |
Correct |
483 ms |
1009804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
1009876 KB |
Output is correct |
2 |
Correct |
479 ms |
1010108 KB |
Output is correct |
3 |
Correct |
479 ms |
1009784 KB |
Output is correct |
4 |
Correct |
481 ms |
1009784 KB |
Output is correct |
5 |
Correct |
488 ms |
1009784 KB |
Output is correct |
6 |
Correct |
477 ms |
1009812 KB |
Output is correct |
7 |
Correct |
481 ms |
1009892 KB |
Output is correct |
8 |
Correct |
478 ms |
1009784 KB |
Output is correct |
9 |
Correct |
483 ms |
1009864 KB |
Output is correct |
10 |
Correct |
479 ms |
1009784 KB |
Output is correct |
11 |
Correct |
479 ms |
1009784 KB |
Output is correct |
12 |
Correct |
480 ms |
1009912 KB |
Output is correct |
13 |
Correct |
483 ms |
1009784 KB |
Output is correct |
14 |
Correct |
484 ms |
1009992 KB |
Output is correct |
15 |
Correct |
482 ms |
1009788 KB |
Output is correct |
16 |
Correct |
478 ms |
1009784 KB |
Output is correct |
17 |
Correct |
483 ms |
1009804 KB |
Output is correct |
18 |
Correct |
481 ms |
1009844 KB |
Output is correct |
19 |
Correct |
497 ms |
1009984 KB |
Output is correct |
20 |
Correct |
485 ms |
1009784 KB |
Output is correct |
21 |
Correct |
479 ms |
1009784 KB |
Output is correct |
22 |
Correct |
485 ms |
1009912 KB |
Output is correct |
23 |
Correct |
474 ms |
1009784 KB |
Output is correct |
24 |
Correct |
481 ms |
1009784 KB |
Output is correct |
25 |
Correct |
485 ms |
1009784 KB |
Output is correct |
26 |
Correct |
479 ms |
1009784 KB |
Output is correct |
27 |
Correct |
479 ms |
1010040 KB |
Output is correct |
28 |
Correct |
472 ms |
1009912 KB |
Output is correct |
29 |
Correct |
476 ms |
1009984 KB |
Output is correct |
30 |
Correct |
485 ms |
1009788 KB |
Output is correct |
31 |
Correct |
475 ms |
1009784 KB |
Output is correct |
32 |
Correct |
480 ms |
1009784 KB |
Output is correct |
33 |
Correct |
473 ms |
1009784 KB |
Output is correct |
34 |
Correct |
484 ms |
1009912 KB |
Output is correct |
35 |
Correct |
480 ms |
1009784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
1009876 KB |
Output is correct |
2 |
Correct |
479 ms |
1010108 KB |
Output is correct |
3 |
Correct |
479 ms |
1009784 KB |
Output is correct |
4 |
Correct |
481 ms |
1009784 KB |
Output is correct |
5 |
Correct |
488 ms |
1009784 KB |
Output is correct |
6 |
Correct |
477 ms |
1009812 KB |
Output is correct |
7 |
Correct |
481 ms |
1009892 KB |
Output is correct |
8 |
Correct |
478 ms |
1009784 KB |
Output is correct |
9 |
Correct |
483 ms |
1009864 KB |
Output is correct |
10 |
Correct |
479 ms |
1009784 KB |
Output is correct |
11 |
Correct |
479 ms |
1009784 KB |
Output is correct |
12 |
Correct |
480 ms |
1009912 KB |
Output is correct |
13 |
Correct |
483 ms |
1009784 KB |
Output is correct |
14 |
Correct |
484 ms |
1009992 KB |
Output is correct |
15 |
Correct |
482 ms |
1009788 KB |
Output is correct |
16 |
Correct |
478 ms |
1009784 KB |
Output is correct |
17 |
Correct |
483 ms |
1009804 KB |
Output is correct |
18 |
Correct |
517 ms |
1009948 KB |
Output is correct |
19 |
Correct |
491 ms |
1009784 KB |
Output is correct |
20 |
Correct |
483 ms |
1009920 KB |
Output is correct |
21 |
Correct |
506 ms |
1009784 KB |
Output is correct |
22 |
Correct |
504 ms |
1009784 KB |
Output is correct |
23 |
Correct |
482 ms |
1009784 KB |
Output is correct |
24 |
Correct |
487 ms |
1010016 KB |
Output is correct |
25 |
Correct |
497 ms |
1009784 KB |
Output is correct |
26 |
Correct |
482 ms |
1010048 KB |
Output is correct |
27 |
Correct |
497 ms |
1009788 KB |
Output is correct |
28 |
Correct |
492 ms |
1009784 KB |
Output is correct |
29 |
Correct |
489 ms |
1010040 KB |
Output is correct |
30 |
Correct |
494 ms |
1009784 KB |
Output is correct |
31 |
Correct |
485 ms |
1009912 KB |
Output is correct |
32 |
Correct |
479 ms |
1009784 KB |
Output is correct |
33 |
Correct |
483 ms |
1009784 KB |
Output is correct |
34 |
Correct |
494 ms |
1009912 KB |
Output is correct |
35 |
Correct |
493 ms |
1009760 KB |
Output is correct |
36 |
Correct |
488 ms |
1010040 KB |
Output is correct |
37 |
Correct |
491 ms |
1009784 KB |
Output is correct |
38 |
Correct |
482 ms |
1009784 KB |
Output is correct |
39 |
Correct |
487 ms |
1009784 KB |
Output is correct |
40 |
Correct |
480 ms |
1009788 KB |
Output is correct |
41 |
Correct |
552 ms |
1010120 KB |
Output is correct |
42 |
Correct |
507 ms |
1009764 KB |
Output is correct |
43 |
Correct |
533 ms |
1009784 KB |
Output is correct |
44 |
Correct |
483 ms |
1009784 KB |
Output is correct |
45 |
Correct |
543 ms |
1009788 KB |
Output is correct |
46 |
Correct |
485 ms |
1009784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
1009876 KB |
Output is correct |
2 |
Correct |
479 ms |
1010108 KB |
Output is correct |
3 |
Correct |
479 ms |
1009784 KB |
Output is correct |
4 |
Correct |
481 ms |
1009784 KB |
Output is correct |
5 |
Correct |
488 ms |
1009784 KB |
Output is correct |
6 |
Correct |
477 ms |
1009812 KB |
Output is correct |
7 |
Correct |
481 ms |
1009892 KB |
Output is correct |
8 |
Correct |
478 ms |
1009784 KB |
Output is correct |
9 |
Correct |
483 ms |
1009864 KB |
Output is correct |
10 |
Correct |
479 ms |
1009784 KB |
Output is correct |
11 |
Correct |
479 ms |
1009784 KB |
Output is correct |
12 |
Correct |
480 ms |
1009912 KB |
Output is correct |
13 |
Correct |
483 ms |
1009784 KB |
Output is correct |
14 |
Correct |
484 ms |
1009992 KB |
Output is correct |
15 |
Correct |
482 ms |
1009788 KB |
Output is correct |
16 |
Correct |
478 ms |
1009784 KB |
Output is correct |
17 |
Correct |
483 ms |
1009804 KB |
Output is correct |
18 |
Correct |
481 ms |
1009844 KB |
Output is correct |
19 |
Correct |
497 ms |
1009984 KB |
Output is correct |
20 |
Correct |
485 ms |
1009784 KB |
Output is correct |
21 |
Correct |
479 ms |
1009784 KB |
Output is correct |
22 |
Correct |
485 ms |
1009912 KB |
Output is correct |
23 |
Correct |
474 ms |
1009784 KB |
Output is correct |
24 |
Correct |
481 ms |
1009784 KB |
Output is correct |
25 |
Correct |
485 ms |
1009784 KB |
Output is correct |
26 |
Correct |
479 ms |
1009784 KB |
Output is correct |
27 |
Correct |
479 ms |
1010040 KB |
Output is correct |
28 |
Correct |
472 ms |
1009912 KB |
Output is correct |
29 |
Correct |
476 ms |
1009984 KB |
Output is correct |
30 |
Correct |
485 ms |
1009788 KB |
Output is correct |
31 |
Correct |
475 ms |
1009784 KB |
Output is correct |
32 |
Correct |
480 ms |
1009784 KB |
Output is correct |
33 |
Correct |
473 ms |
1009784 KB |
Output is correct |
34 |
Correct |
484 ms |
1009912 KB |
Output is correct |
35 |
Correct |
480 ms |
1009784 KB |
Output is correct |
36 |
Correct |
517 ms |
1009948 KB |
Output is correct |
37 |
Correct |
491 ms |
1009784 KB |
Output is correct |
38 |
Correct |
483 ms |
1009920 KB |
Output is correct |
39 |
Correct |
506 ms |
1009784 KB |
Output is correct |
40 |
Correct |
504 ms |
1009784 KB |
Output is correct |
41 |
Correct |
482 ms |
1009784 KB |
Output is correct |
42 |
Correct |
487 ms |
1010016 KB |
Output is correct |
43 |
Correct |
497 ms |
1009784 KB |
Output is correct |
44 |
Correct |
482 ms |
1010048 KB |
Output is correct |
45 |
Correct |
497 ms |
1009788 KB |
Output is correct |
46 |
Correct |
492 ms |
1009784 KB |
Output is correct |
47 |
Correct |
489 ms |
1010040 KB |
Output is correct |
48 |
Correct |
494 ms |
1009784 KB |
Output is correct |
49 |
Correct |
485 ms |
1009912 KB |
Output is correct |
50 |
Correct |
479 ms |
1009784 KB |
Output is correct |
51 |
Correct |
483 ms |
1009784 KB |
Output is correct |
52 |
Correct |
494 ms |
1009912 KB |
Output is correct |
53 |
Correct |
493 ms |
1009760 KB |
Output is correct |
54 |
Correct |
488 ms |
1010040 KB |
Output is correct |
55 |
Correct |
491 ms |
1009784 KB |
Output is correct |
56 |
Correct |
482 ms |
1009784 KB |
Output is correct |
57 |
Correct |
487 ms |
1009784 KB |
Output is correct |
58 |
Correct |
480 ms |
1009788 KB |
Output is correct |
59 |
Correct |
552 ms |
1010120 KB |
Output is correct |
60 |
Correct |
507 ms |
1009764 KB |
Output is correct |
61 |
Correct |
533 ms |
1009784 KB |
Output is correct |
62 |
Correct |
483 ms |
1009784 KB |
Output is correct |
63 |
Correct |
543 ms |
1009788 KB |
Output is correct |
64 |
Correct |
485 ms |
1009784 KB |
Output is correct |
65 |
Correct |
523 ms |
1009784 KB |
Output is correct |
66 |
Correct |
516 ms |
1009788 KB |
Output is correct |
67 |
Correct |
519 ms |
1009912 KB |
Output is correct |
68 |
Correct |
523 ms |
1009912 KB |
Output is correct |
69 |
Correct |
523 ms |
1009912 KB |
Output is correct |
70 |
Correct |
538 ms |
1009788 KB |
Output is correct |
71 |
Correct |
520 ms |
1009784 KB |
Output is correct |
72 |
Correct |
530 ms |
1009912 KB |
Output is correct |
73 |
Correct |
517 ms |
1009912 KB |
Output is correct |
74 |
Correct |
527 ms |
1009784 KB |
Output is correct |
75 |
Correct |
516 ms |
1009912 KB |
Output is correct |
76 |
Correct |
545 ms |
1009912 KB |
Output is correct |
77 |
Correct |
550 ms |
1009784 KB |
Output is correct |
78 |
Correct |
524 ms |
1009848 KB |
Output is correct |
79 |
Correct |
532 ms |
1009764 KB |
Output is correct |
80 |
Correct |
536 ms |
1010024 KB |
Output is correct |
81 |
Correct |
512 ms |
1010040 KB |
Output is correct |
82 |
Correct |
505 ms |
1009784 KB |
Output is correct |
83 |
Correct |
525 ms |
1009912 KB |
Output is correct |
84 |
Correct |
508 ms |
1009912 KB |
Output is correct |
85 |
Correct |
510 ms |
1009784 KB |
Output is correct |
86 |
Correct |
521 ms |
1009912 KB |
Output is correct |
87 |
Correct |
525 ms |
1009972 KB |
Output is correct |
88 |
Correct |
543 ms |
1009784 KB |
Output is correct |
89 |
Correct |
543 ms |
1010040 KB |
Output is correct |
90 |
Correct |
492 ms |
1009784 KB |
Output is correct |
91 |
Correct |
544 ms |
1009804 KB |
Output is correct |
92 |
Correct |
549 ms |
1010020 KB |
Output is correct |
93 |
Correct |
487 ms |
1009784 KB |
Output is correct |