#include <bits/stdc++.h>
const int N = 5001, M = 5001;
int dp[N][M][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 i = m; i >= 0; --i) {
for (int j = n; j >= 0; --j) {
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][b1][b2] = b1 * a + (m - i) * b;
continue;
}
dp[i][j][b1][b2] = std::max(dp[i + 1][j][0][b2] + b1 * a + b,
dp[i][j + 1][b1][0] + b2 * a + b);
if (t[i] == s[j]) {
dp[i][j][b1][b2] = std::max(dp[i][j][b1][b2],
v[t[i] - 1] + dp[i + 1][j + 1][1][1]);
}
}
}
if (i == 0 and j != n) {
ans = std::max(ans, dp[0][j][1][1]);
}
}
}
std::cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
23 ms |
17500 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
5208 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
2140 KB |
Output is correct |
9 |
Correct |
10 ms |
10268 KB |
Output is correct |
10 |
Correct |
24 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4184 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
6 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
2 ms |
3160 KB |
Output is correct |
6 |
Correct |
11 ms |
11028 KB |
Output is correct |
7 |
Correct |
10 ms |
11560 KB |
Output is correct |
8 |
Correct |
17 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
21 ms |
20196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
41700 KB |
Output is correct |
2 |
Correct |
16 ms |
16988 KB |
Output is correct |
3 |
Correct |
131 ms |
137488 KB |
Output is correct |
4 |
Runtime error |
258 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
41700 KB |
Output is correct |
2 |
Correct |
16 ms |
16988 KB |
Output is correct |
3 |
Correct |
131 ms |
137488 KB |
Output is correct |
4 |
Runtime error |
258 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
41700 KB |
Output is correct |
2 |
Correct |
16 ms |
16988 KB |
Output is correct |
3 |
Correct |
131 ms |
137488 KB |
Output is correct |
4 |
Runtime error |
258 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
856 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
23 ms |
17500 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
0 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
5208 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
2140 KB |
Output is correct |
9 |
Correct |
10 ms |
10268 KB |
Output is correct |
10 |
Correct |
24 ms |
20060 KB |
Output is correct |
11 |
Correct |
3 ms |
4184 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
6 ms |
7260 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
2 ms |
3160 KB |
Output is correct |
16 |
Correct |
11 ms |
11028 KB |
Output is correct |
17 |
Correct |
10 ms |
11560 KB |
Output is correct |
18 |
Correct |
17 ms |
14684 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
21 ms |
20196 KB |
Output is correct |
21 |
Correct |
41 ms |
41700 KB |
Output is correct |
22 |
Correct |
16 ms |
16988 KB |
Output is correct |
23 |
Correct |
131 ms |
137488 KB |
Output is correct |
24 |
Runtime error |
258 ms |
262144 KB |
Execution killed with signal 9 |
25 |
Halted |
0 ms |
0 KB |
- |