#include <bits/stdc++.h>
using namespace std;
#define mod 1'000'000'007
typedef long long ll;
template<typename T>
void deb(initializer_list<T> l)
{
for (auto &e : l)
cout << e << ' ';
cout << endl;
}
int k, n, m, a, b;
vector<int> v, s, t;
int dp[2][5001][2][2];
void solve()
{
cin >> k >> n >> m >> a >> b;
v.resize(k);
s.resize(n);
t.resize(m);
for (auto &e : v)
cin >> e;
for (auto &e : s)
cin >> e;
for (auto &e : t)
cin >> e;
int ans = -1e9;
int i, j, k, l;
for (i = n; i >= 0; --i)
{
for (j = m; j >= 0; --j)
{
for (k = 0; k < 2; ++k)
{
for (l = 0; l < 2; ++l)
{
if (j == m)
dp[i % 2][j][k][l] = (k + l) * a;
else if (i == n)
dp[i % 2][j][k][l] = k * a + a + (m - j) * b;
else
{
dp[i % 2][j][k][l] = max({k * a + a + (m - j) * b,
b + dp[i % 2][j + 1][k][1],
b + dp[(i + 1) % 2][j][1][l]});
if (s[i] == t[j])
dp[i % 2][j][k][l] = max(dp[i % 2][j][k][l],
dp[(i + 1) % 2][j + 1][0][0] +
(k + l) * a + v[t[j] - 1]);
}
if (!j && !k && !l)
ans = max(ans, dp[i % 2][j][k][l]);
}
}
}
}
// int ans = dp[0][0][0][0];
// for (int i = 1; i < n; ++i)
// ans = max(ans, dp[i][0][0][0]);
cout << ans << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
28 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
17 ms |
384 KB |
Output is correct |
10 |
Correct |
34 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
11 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
17 ms |
384 KB |
Output is correct |
7 |
Correct |
18 ms |
384 KB |
Output is correct |
8 |
Correct |
24 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
31 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
384 KB |
Output is correct |
2 |
Correct |
26 ms |
384 KB |
Output is correct |
3 |
Correct |
180 ms |
512 KB |
Output is correct |
4 |
Correct |
420 ms |
512 KB |
Output is correct |
5 |
Correct |
132 ms |
512 KB |
Output is correct |
6 |
Correct |
192 ms |
512 KB |
Output is correct |
7 |
Correct |
338 ms |
512 KB |
Output is correct |
8 |
Correct |
122 ms |
384 KB |
Output is correct |
9 |
Correct |
235 ms |
632 KB |
Output is correct |
10 |
Correct |
534 ms |
632 KB |
Output is correct |
11 |
Correct |
552 ms |
512 KB |
Output is correct |
12 |
Correct |
537 ms |
632 KB |
Output is correct |
13 |
Correct |
522 ms |
512 KB |
Output is correct |
14 |
Correct |
693 ms |
632 KB |
Output is correct |
15 |
Correct |
678 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
384 KB |
Output is correct |
2 |
Correct |
26 ms |
384 KB |
Output is correct |
3 |
Correct |
180 ms |
512 KB |
Output is correct |
4 |
Correct |
420 ms |
512 KB |
Output is correct |
5 |
Correct |
132 ms |
512 KB |
Output is correct |
6 |
Correct |
192 ms |
512 KB |
Output is correct |
7 |
Correct |
338 ms |
512 KB |
Output is correct |
8 |
Correct |
122 ms |
384 KB |
Output is correct |
9 |
Correct |
235 ms |
632 KB |
Output is correct |
10 |
Correct |
534 ms |
632 KB |
Output is correct |
11 |
Correct |
552 ms |
512 KB |
Output is correct |
12 |
Correct |
537 ms |
632 KB |
Output is correct |
13 |
Correct |
522 ms |
512 KB |
Output is correct |
14 |
Correct |
693 ms |
632 KB |
Output is correct |
15 |
Correct |
678 ms |
512 KB |
Output is correct |
16 |
Correct |
288 ms |
512 KB |
Output is correct |
17 |
Correct |
339 ms |
632 KB |
Output is correct |
18 |
Correct |
305 ms |
512 KB |
Output is correct |
19 |
Correct |
63 ms |
384 KB |
Output is correct |
20 |
Correct |
215 ms |
512 KB |
Output is correct |
21 |
Correct |
16 ms |
384 KB |
Output is correct |
22 |
Correct |
72 ms |
512 KB |
Output is correct |
23 |
Correct |
138 ms |
632 KB |
Output is correct |
24 |
Correct |
53 ms |
384 KB |
Output is correct |
25 |
Correct |
533 ms |
512 KB |
Output is correct |
26 |
Correct |
533 ms |
576 KB |
Output is correct |
27 |
Correct |
533 ms |
512 KB |
Output is correct |
28 |
Correct |
537 ms |
632 KB |
Output is correct |
29 |
Correct |
686 ms |
580 KB |
Output is correct |
30 |
Correct |
687 ms |
584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
384 KB |
Output is correct |
2 |
Correct |
26 ms |
384 KB |
Output is correct |
3 |
Correct |
180 ms |
512 KB |
Output is correct |
4 |
Correct |
420 ms |
512 KB |
Output is correct |
5 |
Correct |
132 ms |
512 KB |
Output is correct |
6 |
Correct |
192 ms |
512 KB |
Output is correct |
7 |
Correct |
338 ms |
512 KB |
Output is correct |
8 |
Correct |
122 ms |
384 KB |
Output is correct |
9 |
Correct |
235 ms |
632 KB |
Output is correct |
10 |
Correct |
534 ms |
632 KB |
Output is correct |
11 |
Correct |
552 ms |
512 KB |
Output is correct |
12 |
Correct |
537 ms |
632 KB |
Output is correct |
13 |
Correct |
522 ms |
512 KB |
Output is correct |
14 |
Correct |
693 ms |
632 KB |
Output is correct |
15 |
Correct |
678 ms |
512 KB |
Output is correct |
16 |
Correct |
456 ms |
608 KB |
Output is correct |
17 |
Correct |
207 ms |
384 KB |
Output is correct |
18 |
Correct |
68 ms |
384 KB |
Output is correct |
19 |
Correct |
23 ms |
384 KB |
Output is correct |
20 |
Correct |
14 ms |
384 KB |
Output is correct |
21 |
Correct |
19 ms |
512 KB |
Output is correct |
22 |
Correct |
203 ms |
512 KB |
Output is correct |
23 |
Correct |
43 ms |
384 KB |
Output is correct |
24 |
Correct |
365 ms |
532 KB |
Output is correct |
25 |
Correct |
513 ms |
512 KB |
Output is correct |
26 |
Correct |
545 ms |
512 KB |
Output is correct |
27 |
Correct |
547 ms |
512 KB |
Output is correct |
28 |
Correct |
547 ms |
632 KB |
Output is correct |
29 |
Correct |
680 ms |
632 KB |
Output is correct |
30 |
Correct |
685 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
28 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
17 ms |
384 KB |
Output is correct |
10 |
Correct |
34 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
11 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
7 ms |
384 KB |
Output is correct |
16 |
Correct |
17 ms |
384 KB |
Output is correct |
17 |
Correct |
18 ms |
384 KB |
Output is correct |
18 |
Correct |
24 ms |
384 KB |
Output is correct |
19 |
Correct |
7 ms |
384 KB |
Output is correct |
20 |
Correct |
31 ms |
384 KB |
Output is correct |
21 |
Correct |
58 ms |
384 KB |
Output is correct |
22 |
Correct |
26 ms |
384 KB |
Output is correct |
23 |
Correct |
180 ms |
512 KB |
Output is correct |
24 |
Correct |
420 ms |
512 KB |
Output is correct |
25 |
Correct |
132 ms |
512 KB |
Output is correct |
26 |
Correct |
192 ms |
512 KB |
Output is correct |
27 |
Correct |
338 ms |
512 KB |
Output is correct |
28 |
Correct |
122 ms |
384 KB |
Output is correct |
29 |
Correct |
235 ms |
632 KB |
Output is correct |
30 |
Correct |
534 ms |
632 KB |
Output is correct |
31 |
Correct |
552 ms |
512 KB |
Output is correct |
32 |
Correct |
537 ms |
632 KB |
Output is correct |
33 |
Correct |
522 ms |
512 KB |
Output is correct |
34 |
Correct |
693 ms |
632 KB |
Output is correct |
35 |
Correct |
678 ms |
512 KB |
Output is correct |
36 |
Correct |
288 ms |
512 KB |
Output is correct |
37 |
Correct |
339 ms |
632 KB |
Output is correct |
38 |
Correct |
305 ms |
512 KB |
Output is correct |
39 |
Correct |
63 ms |
384 KB |
Output is correct |
40 |
Correct |
215 ms |
512 KB |
Output is correct |
41 |
Correct |
16 ms |
384 KB |
Output is correct |
42 |
Correct |
72 ms |
512 KB |
Output is correct |
43 |
Correct |
138 ms |
632 KB |
Output is correct |
44 |
Correct |
53 ms |
384 KB |
Output is correct |
45 |
Correct |
533 ms |
512 KB |
Output is correct |
46 |
Correct |
533 ms |
576 KB |
Output is correct |
47 |
Correct |
533 ms |
512 KB |
Output is correct |
48 |
Correct |
537 ms |
632 KB |
Output is correct |
49 |
Correct |
686 ms |
580 KB |
Output is correct |
50 |
Correct |
687 ms |
584 KB |
Output is correct |
51 |
Correct |
456 ms |
608 KB |
Output is correct |
52 |
Correct |
207 ms |
384 KB |
Output is correct |
53 |
Correct |
68 ms |
384 KB |
Output is correct |
54 |
Correct |
23 ms |
384 KB |
Output is correct |
55 |
Correct |
14 ms |
384 KB |
Output is correct |
56 |
Correct |
19 ms |
512 KB |
Output is correct |
57 |
Correct |
203 ms |
512 KB |
Output is correct |
58 |
Correct |
43 ms |
384 KB |
Output is correct |
59 |
Correct |
365 ms |
532 KB |
Output is correct |
60 |
Correct |
513 ms |
512 KB |
Output is correct |
61 |
Correct |
545 ms |
512 KB |
Output is correct |
62 |
Correct |
547 ms |
512 KB |
Output is correct |
63 |
Correct |
547 ms |
632 KB |
Output is correct |
64 |
Correct |
680 ms |
632 KB |
Output is correct |
65 |
Correct |
685 ms |
512 KB |
Output is correct |
66 |
Correct |
4 ms |
384 KB |
Output is correct |
67 |
Correct |
5 ms |
384 KB |
Output is correct |
68 |
Correct |
4 ms |
384 KB |
Output is correct |
69 |
Correct |
5 ms |
384 KB |
Output is correct |
70 |
Correct |
4 ms |
384 KB |
Output is correct |
71 |
Correct |
5 ms |
512 KB |
Output is correct |
72 |
Correct |
4 ms |
384 KB |
Output is correct |
73 |
Correct |
5 ms |
384 KB |
Output is correct |
74 |
Correct |
5 ms |
384 KB |
Output is correct |
75 |
Correct |
5 ms |
384 KB |
Output is correct |
76 |
Correct |
5 ms |
384 KB |
Output is correct |
77 |
Correct |
5 ms |
384 KB |
Output is correct |
78 |
Correct |
5 ms |
384 KB |
Output is correct |
79 |
Correct |
5 ms |
384 KB |
Output is correct |
80 |
Correct |
6 ms |
384 KB |
Output is correct |
81 |
Correct |
448 ms |
632 KB |
Output is correct |
82 |
Correct |
104 ms |
384 KB |
Output is correct |
83 |
Correct |
192 ms |
512 KB |
Output is correct |
84 |
Correct |
109 ms |
512 KB |
Output is correct |
85 |
Correct |
170 ms |
504 KB |
Output is correct |
86 |
Correct |
151 ms |
512 KB |
Output is correct |
87 |
Correct |
31 ms |
384 KB |
Output is correct |
88 |
Correct |
19 ms |
384 KB |
Output is correct |
89 |
Correct |
63 ms |
384 KB |
Output is correct |
90 |
Correct |
582 ms |
512 KB |
Output is correct |
91 |
Correct |
528 ms |
512 KB |
Output is correct |
92 |
Correct |
539 ms |
580 KB |
Output is correct |
93 |
Correct |
560 ms |
512 KB |
Output is correct |
94 |
Correct |
543 ms |
632 KB |
Output is correct |
95 |
Correct |
540 ms |
512 KB |
Output is correct |
96 |
Correct |
539 ms |
512 KB |
Output is correct |
97 |
Correct |
530 ms |
512 KB |
Output is correct |
98 |
Correct |
694 ms |
512 KB |
Output is correct |
99 |
Correct |
703 ms |
512 KB |
Output is correct |
100 |
Correct |
688 ms |
580 KB |
Output is correct |