#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define f first
#define s second
const long long inf = 1000000000000000007;
const int maxn = 202;
int n, m;
pi a[maxn];
long long dp[maxn][maxn][maxn][2];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i].f;
for(int i = 1; i <= n; i++) cin >> a[i].s;
a[n + 1].f = m;
sort(a + 1, a + n + 1);
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
for(int k = n + 1; k > j; k--) dp[i][j][k][0] = dp[i][j][k][1] = inf;
int ret = 0;
dp[0][0][n + 1][0] = dp[0][0][n + 1][1] = 0;
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
for(int k = n + 1; k > j; k--){
long long x;
if(dp[i][j][k][0] != inf){
ret = i;
x = dp[i][j][k][0] + a[j + 1].f - a[j].f;
dp[i][j + 1][k][0] = min(dp[i][j + 1][k][0], x);
if(x <= a[j + 1].s) dp[i + 1][j + 1][k][0] = min(dp[i + 1][j + 1][k][0], x);
x = dp[i][j][k][0] + a[j].f + m - a[k - 1].f;
dp[i][j][k - 1][1] = min(dp[i][j][k - 1][1], x);
if(x <= a[k - 1].s) dp[i + 1][j][k - 1][1] = min(dp[i + 1][j][k - 1][1], x);
}
if(dp[i][j][k][1] != inf){
ret = i;
x = dp[i][j][k][1] + a[k].f - a[k - 1].f;
dp[i][j][k - 1][1] = min(dp[i][j][k - 1][1], x);
if(x <= a[k - 1].s) dp[i + 1][j][k - 1][1] = min(dp[i + 1][j][k - 1][1], x);
x = dp[i][j][k][1] + a[j + 1].f + m - a[k].f;
dp[i][j + 1][k][0] = min(dp[i][j + 1][k][0], x);
if(x <= a[j + 1].s) dp[i + 1][j + 1][k][0] = min(dp[i + 1][j + 1][k][0], x);
}
}
cout << ret << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
508 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
888 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
888 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
888 KB |
Output is correct |
13 |
Correct |
5 ms |
892 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
5 ms |
1016 KB |
Output is correct |
17 |
Correct |
5 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
508 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
888 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
888 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
888 KB |
Output is correct |
13 |
Correct |
5 ms |
892 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
5 ms |
1016 KB |
Output is correct |
17 |
Correct |
5 ms |
1016 KB |
Output is correct |
18 |
Correct |
5 ms |
1144 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
5 ms |
888 KB |
Output is correct |
21 |
Correct |
5 ms |
1144 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
6 ms |
1148 KB |
Output is correct |
24 |
Correct |
5 ms |
1020 KB |
Output is correct |
25 |
Correct |
5 ms |
1144 KB |
Output is correct |
26 |
Correct |
5 ms |
1144 KB |
Output is correct |
27 |
Correct |
5 ms |
632 KB |
Output is correct |
28 |
Correct |
5 ms |
632 KB |
Output is correct |
29 |
Correct |
5 ms |
1272 KB |
Output is correct |
30 |
Correct |
5 ms |
1276 KB |
Output is correct |
31 |
Correct |
5 ms |
1016 KB |
Output is correct |
32 |
Correct |
5 ms |
1016 KB |
Output is correct |
33 |
Correct |
5 ms |
1272 KB |
Output is correct |
34 |
Correct |
5 ms |
1272 KB |
Output is correct |
35 |
Correct |
5 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
508 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
888 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
888 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
888 KB |
Output is correct |
13 |
Correct |
5 ms |
892 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
5 ms |
1016 KB |
Output is correct |
17 |
Correct |
5 ms |
1016 KB |
Output is correct |
18 |
Correct |
75 ms |
103800 KB |
Output is correct |
19 |
Correct |
46 ms |
63096 KB |
Output is correct |
20 |
Correct |
26 ms |
33784 KB |
Output is correct |
21 |
Correct |
42 ms |
59512 KB |
Output is correct |
22 |
Correct |
58 ms |
77176 KB |
Output is correct |
23 |
Correct |
24 ms |
28792 KB |
Output is correct |
24 |
Correct |
18 ms |
22392 KB |
Output is correct |
25 |
Correct |
23 ms |
28152 KB |
Output is correct |
26 |
Correct |
10 ms |
10744 KB |
Output is correct |
27 |
Correct |
25 ms |
29432 KB |
Output is correct |
28 |
Correct |
16 ms |
20984 KB |
Output is correct |
29 |
Correct |
24 ms |
29944 KB |
Output is correct |
30 |
Correct |
19 ms |
23420 KB |
Output is correct |
31 |
Correct |
22 ms |
28152 KB |
Output is correct |
32 |
Correct |
13 ms |
14840 KB |
Output is correct |
33 |
Correct |
21 ms |
28156 KB |
Output is correct |
34 |
Correct |
10 ms |
9976 KB |
Output is correct |
35 |
Correct |
21 ms |
27384 KB |
Output is correct |
36 |
Correct |
12 ms |
13048 KB |
Output is correct |
37 |
Correct |
21 ms |
29176 KB |
Output is correct |
38 |
Correct |
15 ms |
16120 KB |
Output is correct |
39 |
Correct |
24 ms |
30456 KB |
Output is correct |
40 |
Correct |
14 ms |
17912 KB |
Output is correct |
41 |
Correct |
98 ms |
127992 KB |
Output is correct |
42 |
Correct |
56 ms |
86012 KB |
Output is correct |
43 |
Correct |
99 ms |
127992 KB |
Output is correct |
44 |
Correct |
54 ms |
84984 KB |
Output is correct |
45 |
Correct |
106 ms |
128248 KB |
Output is correct |
46 |
Correct |
55 ms |
86008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
508 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
888 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
888 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
888 KB |
Output is correct |
13 |
Correct |
5 ms |
892 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
5 ms |
1016 KB |
Output is correct |
17 |
Correct |
5 ms |
1016 KB |
Output is correct |
18 |
Correct |
5 ms |
1144 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
5 ms |
888 KB |
Output is correct |
21 |
Correct |
5 ms |
1144 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
6 ms |
1148 KB |
Output is correct |
24 |
Correct |
5 ms |
1020 KB |
Output is correct |
25 |
Correct |
5 ms |
1144 KB |
Output is correct |
26 |
Correct |
5 ms |
1144 KB |
Output is correct |
27 |
Correct |
5 ms |
632 KB |
Output is correct |
28 |
Correct |
5 ms |
632 KB |
Output is correct |
29 |
Correct |
5 ms |
1272 KB |
Output is correct |
30 |
Correct |
5 ms |
1276 KB |
Output is correct |
31 |
Correct |
5 ms |
1016 KB |
Output is correct |
32 |
Correct |
5 ms |
1016 KB |
Output is correct |
33 |
Correct |
5 ms |
1272 KB |
Output is correct |
34 |
Correct |
5 ms |
1272 KB |
Output is correct |
35 |
Correct |
5 ms |
1272 KB |
Output is correct |
36 |
Correct |
75 ms |
103800 KB |
Output is correct |
37 |
Correct |
46 ms |
63096 KB |
Output is correct |
38 |
Correct |
26 ms |
33784 KB |
Output is correct |
39 |
Correct |
42 ms |
59512 KB |
Output is correct |
40 |
Correct |
58 ms |
77176 KB |
Output is correct |
41 |
Correct |
24 ms |
28792 KB |
Output is correct |
42 |
Correct |
18 ms |
22392 KB |
Output is correct |
43 |
Correct |
23 ms |
28152 KB |
Output is correct |
44 |
Correct |
10 ms |
10744 KB |
Output is correct |
45 |
Correct |
25 ms |
29432 KB |
Output is correct |
46 |
Correct |
16 ms |
20984 KB |
Output is correct |
47 |
Correct |
24 ms |
29944 KB |
Output is correct |
48 |
Correct |
19 ms |
23420 KB |
Output is correct |
49 |
Correct |
22 ms |
28152 KB |
Output is correct |
50 |
Correct |
13 ms |
14840 KB |
Output is correct |
51 |
Correct |
21 ms |
28156 KB |
Output is correct |
52 |
Correct |
10 ms |
9976 KB |
Output is correct |
53 |
Correct |
21 ms |
27384 KB |
Output is correct |
54 |
Correct |
12 ms |
13048 KB |
Output is correct |
55 |
Correct |
21 ms |
29176 KB |
Output is correct |
56 |
Correct |
15 ms |
16120 KB |
Output is correct |
57 |
Correct |
24 ms |
30456 KB |
Output is correct |
58 |
Correct |
14 ms |
17912 KB |
Output is correct |
59 |
Correct |
98 ms |
127992 KB |
Output is correct |
60 |
Correct |
56 ms |
86012 KB |
Output is correct |
61 |
Correct |
99 ms |
127992 KB |
Output is correct |
62 |
Correct |
54 ms |
84984 KB |
Output is correct |
63 |
Correct |
106 ms |
128248 KB |
Output is correct |
64 |
Correct |
55 ms |
86008 KB |
Output is correct |
65 |
Correct |
84 ms |
115576 KB |
Output is correct |
66 |
Correct |
78 ms |
106104 KB |
Output is correct |
67 |
Correct |
77 ms |
101496 KB |
Output is correct |
68 |
Correct |
71 ms |
93816 KB |
Output is correct |
69 |
Correct |
83 ms |
114424 KB |
Output is correct |
70 |
Correct |
89 ms |
109816 KB |
Output is correct |
71 |
Correct |
76 ms |
110584 KB |
Output is correct |
72 |
Correct |
94 ms |
112120 KB |
Output is correct |
73 |
Correct |
70 ms |
103928 KB |
Output is correct |
74 |
Correct |
79 ms |
97144 KB |
Output is correct |
75 |
Correct |
73 ms |
107128 KB |
Output is correct |
76 |
Correct |
97 ms |
122876 KB |
Output is correct |
77 |
Correct |
116 ms |
122872 KB |
Output is correct |
78 |
Correct |
73 ms |
94840 KB |
Output is correct |
79 |
Correct |
71 ms |
98168 KB |
Output is correct |
80 |
Correct |
95 ms |
120440 KB |
Output is correct |
81 |
Correct |
68 ms |
99168 KB |
Output is correct |
82 |
Correct |
71 ms |
104824 KB |
Output is correct |
83 |
Correct |
89 ms |
127840 KB |
Output is correct |
84 |
Correct |
80 ms |
109432 KB |
Output is correct |
85 |
Correct |
81 ms |
119288 KB |
Output is correct |
86 |
Correct |
83 ms |
116600 KB |
Output is correct |
87 |
Correct |
78 ms |
107256 KB |
Output is correct |
88 |
Correct |
105 ms |
128760 KB |
Output is correct |
89 |
Correct |
99 ms |
128760 KB |
Output is correct |
90 |
Correct |
69 ms |
108152 KB |
Output is correct |
91 |
Correct |
104 ms |
128760 KB |
Output is correct |
92 |
Correct |
107 ms |
128760 KB |
Output is correct |
93 |
Correct |
79 ms |
126328 KB |
Output is correct |