#include <bits/stdc++.h>
const int N = 5001;
int dp[N][2][2][2];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int k, n, m, a, b;
std::cin >> k >> n >> m >> a >> b;
std::vector<int> v(k), s(n), t(m);
for (auto &i : v) {
std::cin >> i;
}
for (auto &i : s) {
std::cin >> i;
}
for (auto &i : t) {
std::cin >> i;
}
// dp[i][j][b1][b2] = the maximum happiness when we start from index i in t
// and j in s. b1 is 1 if we picked the last element in t, else 0. same for
// b2.
int ans = a + m * b;
for (int j = n; j >= 0; --j) {
for (int i = m; i >= 0; --i) {
for (int b1 = 0; b1 <= 1; ++b1) {
for (int b2 = 0; b2 <= 1; ++b2) {
if (i == m and j == n) {
continue;
}
if (i == m) {
continue;
}
if (j == n) {
dp[i][j % 2][b1][b2] = b1 * a + (m - i) * b;
continue;
}
dp[i][j % 2][b1][b2] =
std::max(dp[i + 1][j % 2][0][b2] + b1 * a + b,
dp[i][(j + 1) % 2][b1][0] + b2 * a + b);
if (t[i] == s[j]) {
dp[i][j % 2][b1][b2] =
std::max(dp[i][j % 2][b1][b2],
v[t[i] - 1] + dp[i + 1][(j + 1) % 2][1][1]);
}
if (i == 0 and j != n and b1 == 1 and b2 == 1) {
ans = std::max(ans, dp[0][j % 2][1][1]);
}
}
}
}
}
std::cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
7 ms |
492 KB |
Output is correct |
10 |
Correct |
19 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
348 KB |
Output is correct |
8 |
Correct |
11 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
13 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
348 KB |
Output is correct |
2 |
Correct |
11 ms |
344 KB |
Output is correct |
3 |
Correct |
91 ms |
348 KB |
Output is correct |
4 |
Correct |
224 ms |
604 KB |
Output is correct |
5 |
Correct |
90 ms |
600 KB |
Output is correct |
6 |
Correct |
93 ms |
604 KB |
Output is correct |
7 |
Correct |
176 ms |
600 KB |
Output is correct |
8 |
Correct |
65 ms |
348 KB |
Output is correct |
9 |
Correct |
114 ms |
600 KB |
Output is correct |
10 |
Correct |
274 ms |
600 KB |
Output is correct |
11 |
Correct |
278 ms |
600 KB |
Output is correct |
12 |
Correct |
320 ms |
672 KB |
Output is correct |
13 |
Correct |
275 ms |
600 KB |
Output is correct |
14 |
Correct |
346 ms |
600 KB |
Output is correct |
15 |
Correct |
328 ms |
648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
348 KB |
Output is correct |
2 |
Correct |
11 ms |
344 KB |
Output is correct |
3 |
Correct |
91 ms |
348 KB |
Output is correct |
4 |
Correct |
224 ms |
604 KB |
Output is correct |
5 |
Correct |
90 ms |
600 KB |
Output is correct |
6 |
Correct |
93 ms |
604 KB |
Output is correct |
7 |
Correct |
176 ms |
600 KB |
Output is correct |
8 |
Correct |
65 ms |
348 KB |
Output is correct |
9 |
Correct |
114 ms |
600 KB |
Output is correct |
10 |
Correct |
274 ms |
600 KB |
Output is correct |
11 |
Correct |
278 ms |
600 KB |
Output is correct |
12 |
Correct |
320 ms |
672 KB |
Output is correct |
13 |
Correct |
275 ms |
600 KB |
Output is correct |
14 |
Correct |
346 ms |
600 KB |
Output is correct |
15 |
Correct |
328 ms |
648 KB |
Output is correct |
16 |
Correct |
151 ms |
600 KB |
Output is correct |
17 |
Correct |
171 ms |
604 KB |
Output is correct |
18 |
Correct |
159 ms |
604 KB |
Output is correct |
19 |
Correct |
42 ms |
348 KB |
Output is correct |
20 |
Correct |
110 ms |
560 KB |
Output is correct |
21 |
Correct |
7 ms |
348 KB |
Output is correct |
22 |
Correct |
35 ms |
604 KB |
Output is correct |
23 |
Correct |
69 ms |
604 KB |
Output is correct |
24 |
Correct |
25 ms |
348 KB |
Output is correct |
25 |
Correct |
281 ms |
668 KB |
Output is correct |
26 |
Correct |
291 ms |
600 KB |
Output is correct |
27 |
Correct |
299 ms |
680 KB |
Output is correct |
28 |
Correct |
287 ms |
600 KB |
Output is correct |
29 |
Correct |
324 ms |
848 KB |
Output is correct |
30 |
Correct |
320 ms |
648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
348 KB |
Output is correct |
2 |
Correct |
11 ms |
344 KB |
Output is correct |
3 |
Correct |
91 ms |
348 KB |
Output is correct |
4 |
Correct |
224 ms |
604 KB |
Output is correct |
5 |
Correct |
90 ms |
600 KB |
Output is correct |
6 |
Correct |
93 ms |
604 KB |
Output is correct |
7 |
Correct |
176 ms |
600 KB |
Output is correct |
8 |
Correct |
65 ms |
348 KB |
Output is correct |
9 |
Correct |
114 ms |
600 KB |
Output is correct |
10 |
Correct |
274 ms |
600 KB |
Output is correct |
11 |
Correct |
278 ms |
600 KB |
Output is correct |
12 |
Correct |
320 ms |
672 KB |
Output is correct |
13 |
Correct |
275 ms |
600 KB |
Output is correct |
14 |
Correct |
346 ms |
600 KB |
Output is correct |
15 |
Correct |
328 ms |
648 KB |
Output is correct |
16 |
Correct |
227 ms |
600 KB |
Output is correct |
17 |
Correct |
117 ms |
604 KB |
Output is correct |
18 |
Correct |
33 ms |
344 KB |
Output is correct |
19 |
Correct |
9 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
604 KB |
Output is correct |
22 |
Correct |
105 ms |
604 KB |
Output is correct |
23 |
Correct |
20 ms |
344 KB |
Output is correct |
24 |
Correct |
194 ms |
600 KB |
Output is correct |
25 |
Correct |
278 ms |
600 KB |
Output is correct |
26 |
Correct |
344 ms |
680 KB |
Output is correct |
27 |
Correct |
278 ms |
604 KB |
Output is correct |
28 |
Correct |
269 ms |
600 KB |
Output is correct |
29 |
Correct |
334 ms |
644 KB |
Output is correct |
30 |
Correct |
317 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
7 ms |
492 KB |
Output is correct |
10 |
Correct |
19 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
348 KB |
Output is correct |
17 |
Correct |
6 ms |
348 KB |
Output is correct |
18 |
Correct |
11 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
13 ms |
504 KB |
Output is correct |
21 |
Correct |
27 ms |
348 KB |
Output is correct |
22 |
Correct |
11 ms |
344 KB |
Output is correct |
23 |
Correct |
91 ms |
348 KB |
Output is correct |
24 |
Correct |
224 ms |
604 KB |
Output is correct |
25 |
Correct |
90 ms |
600 KB |
Output is correct |
26 |
Correct |
93 ms |
604 KB |
Output is correct |
27 |
Correct |
176 ms |
600 KB |
Output is correct |
28 |
Correct |
65 ms |
348 KB |
Output is correct |
29 |
Correct |
114 ms |
600 KB |
Output is correct |
30 |
Correct |
274 ms |
600 KB |
Output is correct |
31 |
Correct |
278 ms |
600 KB |
Output is correct |
32 |
Correct |
320 ms |
672 KB |
Output is correct |
33 |
Correct |
275 ms |
600 KB |
Output is correct |
34 |
Correct |
346 ms |
600 KB |
Output is correct |
35 |
Correct |
328 ms |
648 KB |
Output is correct |
36 |
Correct |
151 ms |
600 KB |
Output is correct |
37 |
Correct |
171 ms |
604 KB |
Output is correct |
38 |
Correct |
159 ms |
604 KB |
Output is correct |
39 |
Correct |
42 ms |
348 KB |
Output is correct |
40 |
Correct |
110 ms |
560 KB |
Output is correct |
41 |
Correct |
7 ms |
348 KB |
Output is correct |
42 |
Correct |
35 ms |
604 KB |
Output is correct |
43 |
Correct |
69 ms |
604 KB |
Output is correct |
44 |
Correct |
25 ms |
348 KB |
Output is correct |
45 |
Correct |
281 ms |
668 KB |
Output is correct |
46 |
Correct |
291 ms |
600 KB |
Output is correct |
47 |
Correct |
299 ms |
680 KB |
Output is correct |
48 |
Correct |
287 ms |
600 KB |
Output is correct |
49 |
Correct |
324 ms |
848 KB |
Output is correct |
50 |
Correct |
320 ms |
648 KB |
Output is correct |
51 |
Correct |
227 ms |
600 KB |
Output is correct |
52 |
Correct |
117 ms |
604 KB |
Output is correct |
53 |
Correct |
33 ms |
344 KB |
Output is correct |
54 |
Correct |
9 ms |
348 KB |
Output is correct |
55 |
Correct |
4 ms |
348 KB |
Output is correct |
56 |
Correct |
7 ms |
604 KB |
Output is correct |
57 |
Correct |
105 ms |
604 KB |
Output is correct |
58 |
Correct |
20 ms |
344 KB |
Output is correct |
59 |
Correct |
194 ms |
600 KB |
Output is correct |
60 |
Correct |
278 ms |
600 KB |
Output is correct |
61 |
Correct |
344 ms |
680 KB |
Output is correct |
62 |
Correct |
278 ms |
604 KB |
Output is correct |
63 |
Correct |
269 ms |
600 KB |
Output is correct |
64 |
Correct |
334 ms |
644 KB |
Output is correct |
65 |
Correct |
317 ms |
600 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
344 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
344 KB |
Output is correct |
76 |
Correct |
1 ms |
360 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
227 ms |
640 KB |
Output is correct |
82 |
Correct |
51 ms |
528 KB |
Output is correct |
83 |
Correct |
96 ms |
600 KB |
Output is correct |
84 |
Correct |
51 ms |
576 KB |
Output is correct |
85 |
Correct |
92 ms |
344 KB |
Output is correct |
86 |
Correct |
74 ms |
540 KB |
Output is correct |
87 |
Correct |
13 ms |
344 KB |
Output is correct |
88 |
Correct |
10 ms |
344 KB |
Output is correct |
89 |
Correct |
30 ms |
348 KB |
Output is correct |
90 |
Correct |
304 ms |
600 KB |
Output is correct |
91 |
Correct |
288 ms |
604 KB |
Output is correct |
92 |
Correct |
304 ms |
664 KB |
Output is correct |
93 |
Correct |
277 ms |
604 KB |
Output is correct |
94 |
Correct |
277 ms |
604 KB |
Output is correct |
95 |
Correct |
270 ms |
600 KB |
Output is correct |
96 |
Correct |
288 ms |
604 KB |
Output is correct |
97 |
Correct |
289 ms |
600 KB |
Output is correct |
98 |
Correct |
330 ms |
604 KB |
Output is correct |
99 |
Correct |
328 ms |
600 KB |
Output is correct |
100 |
Correct |
332 ms |
604 KB |
Output is correct |