#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[5001][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 |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
18 ms |
384 KB |
Output is correct |
10 |
Correct |
32 ms |
440 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 |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
18 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 |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
32 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
384 KB |
Output is correct |
2 |
Correct |
25 ms |
384 KB |
Output is correct |
3 |
Correct |
185 ms |
504 KB |
Output is correct |
4 |
Correct |
438 ms |
604 KB |
Output is correct |
5 |
Correct |
135 ms |
512 KB |
Output is correct |
6 |
Correct |
183 ms |
556 KB |
Output is correct |
7 |
Correct |
337 ms |
632 KB |
Output is correct |
8 |
Correct |
116 ms |
384 KB |
Output is correct |
9 |
Correct |
213 ms |
512 KB |
Output is correct |
10 |
Correct |
550 ms |
632 KB |
Output is correct |
11 |
Correct |
538 ms |
632 KB |
Output is correct |
12 |
Correct |
528 ms |
632 KB |
Output is correct |
13 |
Correct |
529 ms |
612 KB |
Output is correct |
14 |
Correct |
691 ms |
636 KB |
Output is correct |
15 |
Correct |
685 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
384 KB |
Output is correct |
2 |
Correct |
25 ms |
384 KB |
Output is correct |
3 |
Correct |
185 ms |
504 KB |
Output is correct |
4 |
Correct |
438 ms |
604 KB |
Output is correct |
5 |
Correct |
135 ms |
512 KB |
Output is correct |
6 |
Correct |
183 ms |
556 KB |
Output is correct |
7 |
Correct |
337 ms |
632 KB |
Output is correct |
8 |
Correct |
116 ms |
384 KB |
Output is correct |
9 |
Correct |
213 ms |
512 KB |
Output is correct |
10 |
Correct |
550 ms |
632 KB |
Output is correct |
11 |
Correct |
538 ms |
632 KB |
Output is correct |
12 |
Correct |
528 ms |
632 KB |
Output is correct |
13 |
Correct |
529 ms |
612 KB |
Output is correct |
14 |
Correct |
691 ms |
636 KB |
Output is correct |
15 |
Correct |
685 ms |
632 KB |
Output is correct |
16 |
Correct |
290 ms |
632 KB |
Output is correct |
17 |
Correct |
332 ms |
564 KB |
Output is correct |
18 |
Correct |
306 ms |
512 KB |
Output is correct |
19 |
Correct |
67 ms |
632 KB |
Output is correct |
20 |
Correct |
220 ms |
632 KB |
Output is correct |
21 |
Correct |
22 ms |
384 KB |
Output is correct |
22 |
Correct |
69 ms |
512 KB |
Output is correct |
23 |
Correct |
138 ms |
512 KB |
Output is correct |
24 |
Correct |
51 ms |
384 KB |
Output is correct |
25 |
Correct |
539 ms |
640 KB |
Output is correct |
26 |
Correct |
556 ms |
736 KB |
Output is correct |
27 |
Correct |
535 ms |
620 KB |
Output is correct |
28 |
Correct |
529 ms |
612 KB |
Output is correct |
29 |
Correct |
688 ms |
632 KB |
Output is correct |
30 |
Correct |
676 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
384 KB |
Output is correct |
2 |
Correct |
25 ms |
384 KB |
Output is correct |
3 |
Correct |
185 ms |
504 KB |
Output is correct |
4 |
Correct |
438 ms |
604 KB |
Output is correct |
5 |
Correct |
135 ms |
512 KB |
Output is correct |
6 |
Correct |
183 ms |
556 KB |
Output is correct |
7 |
Correct |
337 ms |
632 KB |
Output is correct |
8 |
Correct |
116 ms |
384 KB |
Output is correct |
9 |
Correct |
213 ms |
512 KB |
Output is correct |
10 |
Correct |
550 ms |
632 KB |
Output is correct |
11 |
Correct |
538 ms |
632 KB |
Output is correct |
12 |
Correct |
528 ms |
632 KB |
Output is correct |
13 |
Correct |
529 ms |
612 KB |
Output is correct |
14 |
Correct |
691 ms |
636 KB |
Output is correct |
15 |
Correct |
685 ms |
632 KB |
Output is correct |
16 |
Correct |
462 ms |
632 KB |
Output is correct |
17 |
Correct |
229 ms |
632 KB |
Output is correct |
18 |
Correct |
70 ms |
504 KB |
Output is correct |
19 |
Correct |
20 ms |
384 KB |
Output is correct |
20 |
Correct |
13 ms |
384 KB |
Output is correct |
21 |
Correct |
17 ms |
512 KB |
Output is correct |
22 |
Correct |
206 ms |
632 KB |
Output is correct |
23 |
Correct |
42 ms |
384 KB |
Output is correct |
24 |
Correct |
368 ms |
512 KB |
Output is correct |
25 |
Correct |
542 ms |
632 KB |
Output is correct |
26 |
Correct |
549 ms |
792 KB |
Output is correct |
27 |
Correct |
558 ms |
512 KB |
Output is correct |
28 |
Correct |
564 ms |
632 KB |
Output is correct |
29 |
Correct |
706 ms |
640 KB |
Output is correct |
30 |
Correct |
691 ms |
632 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 |
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 |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 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 |
5 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 |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
18 ms |
384 KB |
Output is correct |
10 |
Correct |
32 ms |
440 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 |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
18 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 |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
32 ms |
384 KB |
Output is correct |
21 |
Correct |
62 ms |
384 KB |
Output is correct |
22 |
Correct |
25 ms |
384 KB |
Output is correct |
23 |
Correct |
185 ms |
504 KB |
Output is correct |
24 |
Correct |
438 ms |
604 KB |
Output is correct |
25 |
Correct |
135 ms |
512 KB |
Output is correct |
26 |
Correct |
183 ms |
556 KB |
Output is correct |
27 |
Correct |
337 ms |
632 KB |
Output is correct |
28 |
Correct |
116 ms |
384 KB |
Output is correct |
29 |
Correct |
213 ms |
512 KB |
Output is correct |
30 |
Correct |
550 ms |
632 KB |
Output is correct |
31 |
Correct |
538 ms |
632 KB |
Output is correct |
32 |
Correct |
528 ms |
632 KB |
Output is correct |
33 |
Correct |
529 ms |
612 KB |
Output is correct |
34 |
Correct |
691 ms |
636 KB |
Output is correct |
35 |
Correct |
685 ms |
632 KB |
Output is correct |
36 |
Correct |
290 ms |
632 KB |
Output is correct |
37 |
Correct |
332 ms |
564 KB |
Output is correct |
38 |
Correct |
306 ms |
512 KB |
Output is correct |
39 |
Correct |
67 ms |
632 KB |
Output is correct |
40 |
Correct |
220 ms |
632 KB |
Output is correct |
41 |
Correct |
22 ms |
384 KB |
Output is correct |
42 |
Correct |
69 ms |
512 KB |
Output is correct |
43 |
Correct |
138 ms |
512 KB |
Output is correct |
44 |
Correct |
51 ms |
384 KB |
Output is correct |
45 |
Correct |
539 ms |
640 KB |
Output is correct |
46 |
Correct |
556 ms |
736 KB |
Output is correct |
47 |
Correct |
535 ms |
620 KB |
Output is correct |
48 |
Correct |
529 ms |
612 KB |
Output is correct |
49 |
Correct |
688 ms |
632 KB |
Output is correct |
50 |
Correct |
676 ms |
512 KB |
Output is correct |
51 |
Correct |
462 ms |
632 KB |
Output is correct |
52 |
Correct |
229 ms |
632 KB |
Output is correct |
53 |
Correct |
70 ms |
504 KB |
Output is correct |
54 |
Correct |
20 ms |
384 KB |
Output is correct |
55 |
Correct |
13 ms |
384 KB |
Output is correct |
56 |
Correct |
17 ms |
512 KB |
Output is correct |
57 |
Correct |
206 ms |
632 KB |
Output is correct |
58 |
Correct |
42 ms |
384 KB |
Output is correct |
59 |
Correct |
368 ms |
512 KB |
Output is correct |
60 |
Correct |
542 ms |
632 KB |
Output is correct |
61 |
Correct |
549 ms |
792 KB |
Output is correct |
62 |
Correct |
558 ms |
512 KB |
Output is correct |
63 |
Correct |
564 ms |
632 KB |
Output is correct |
64 |
Correct |
706 ms |
640 KB |
Output is correct |
65 |
Correct |
691 ms |
632 KB |
Output is correct |
66 |
Correct |
5 ms |
384 KB |
Output is correct |
67 |
Correct |
5 ms |
384 KB |
Output is correct |
68 |
Correct |
5 ms |
384 KB |
Output is correct |
69 |
Correct |
5 ms |
384 KB |
Output is correct |
70 |
Correct |
5 ms |
384 KB |
Output is correct |
71 |
Correct |
5 ms |
384 KB |
Output is correct |
72 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
81 |
Correct |
453 ms |
632 KB |
Output is correct |
82 |
Correct |
99 ms |
384 KB |
Output is correct |
83 |
Correct |
209 ms |
512 KB |
Output is correct |
84 |
Correct |
108 ms |
512 KB |
Output is correct |
85 |
Correct |
177 ms |
512 KB |
Output is correct |
86 |
Correct |
153 ms |
504 KB |
Output is correct |
87 |
Correct |
29 ms |
384 KB |
Output is correct |
88 |
Correct |
20 ms |
384 KB |
Output is correct |
89 |
Correct |
66 ms |
384 KB |
Output is correct |
90 |
Correct |
607 ms |
604 KB |
Output is correct |
91 |
Correct |
535 ms |
760 KB |
Output is correct |
92 |
Correct |
530 ms |
512 KB |
Output is correct |
93 |
Correct |
525 ms |
632 KB |
Output is correct |
94 |
Correct |
581 ms |
640 KB |
Output is correct |
95 |
Correct |
567 ms |
736 KB |
Output is correct |
96 |
Correct |
548 ms |
736 KB |
Output is correct |
97 |
Correct |
565 ms |
632 KB |
Output is correct |
98 |
Correct |
706 ms |
608 KB |
Output is correct |
99 |
Correct |
724 ms |
632 KB |
Output is correct |
100 |
Correct |
701 ms |
640 KB |
Output is correct |