#include <bits/stdc++.h>
int dp[2][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 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 % 2][j % 2][b1][b2] = b1 * a + (m - i) * b;
continue;
}
dp[i % 2][j % 2][b1][b2] =
std::max(dp[(i + 1) % 2][j % 2][0][b2] + b1 * a + b,
dp[i % 2][(j + 1) % 2][b1][0] + b2 * a + b);
if (t[i] == s[j]) {
dp[i % 2][j % 2][b1][b2] =
std::max(dp[i % 2][j % 2][b1][b2],
v[t[i] - 1] + dp[(i + 1) % 2][(j + 1) % 2][1][1]);
}
}
}
if (i == 0 and j != n) {
ans = std::max(ans, dp[0][j % 2][1][1]);
}
}
}
std::cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
7 ms |
460 KB |
Output is correct |
10 |
Correct |
13 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
7 ms |
460 KB |
Output is correct |
10 |
Correct |
13 ms |
348 KB |
Output is correct |
11 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |