#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
const int MAX_N = 5000, INF = 1e9;
int dp[2][1 + MAX_N][2][2];
int V[1 + MAX_N];
int S[1 + MAX_N];
int T[1 + MAX_N];
int K, A, B, n, m;
void upd (int &a, int b) {
if (a < b)
a = b;
}
void recurence (int i, int j, int bi, int bj) {
if (i < n && j < m && S[i + 1] == T[j + 1])
upd (dp[1 - (i & 1)][j + 1][0][0], dp[i & 1][j][bi][bj] + V[S[i + 1]]);
if (i < n)
upd (dp[1 - (i & 1)][j][1][bj], dp[i & 1][j][bi][bj] + B + ((bi == 0) ? A : 0));
if (j < m)
upd (dp[i & 1][j + 1][bi][1], dp[i & 1][j][bi][bj] + B + ((bj == 0) ? A : 0));
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> K >> n >> m >> A >> B;
for (int i = 1; i <= K; i++)
cin >> V[i];
for (int i = 1; i <= n; i++)
cin >> S[i];
for (int i = 1; i <= m; i++)
cin >> T[i];
for (int j = 0; j <= m; j++)
for (int bi = 0; bi < 2; bi++)
for (int bj = 0; bj < 2; bj++)
dp[0][j][bi][bj] = -INF;
dp[0][0][0][0] = 0;
int ans = A + m * B;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++)
for (int bi = 0; bi < 2; bi++)
for (int bj = 0; bj < 2; bj++)
dp[1 - (i & 1)][j][bi][bj] = -INF;
dp[1 - (i & 1)][0][0][0] = 0;
for (int j = 0; j <= m; j++)
for (int bi = 0; bi < 2; bi++)
for (int bj = 0; bj < 2; bj++)
if (dp[i & 1][j][bi][bj] != -INF)
recurence (i, j, bi, bj);
for (int bi = 0; bi < 2; bi++)
for (int bj = 0; bj < 2; bj++)
upd (ans, dp[i & 1][m][bi][bj]);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
36 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
3 ms |
364 KB |
Output is correct |
9 |
Correct |
20 ms |
492 KB |
Output is correct |
10 |
Correct |
43 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
10 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
4 ms |
364 KB |
Output is correct |
6 |
Correct |
21 ms |
384 KB |
Output is correct |
7 |
Correct |
21 ms |
364 KB |
Output is correct |
8 |
Correct |
32 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
364 KB |
Output is correct |
10 |
Correct |
42 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
384 KB |
Output is correct |
2 |
Correct |
30 ms |
364 KB |
Output is correct |
3 |
Correct |
250 ms |
492 KB |
Output is correct |
4 |
Correct |
603 ms |
620 KB |
Output is correct |
5 |
Correct |
178 ms |
492 KB |
Output is correct |
6 |
Correct |
258 ms |
492 KB |
Output is correct |
7 |
Correct |
480 ms |
492 KB |
Output is correct |
8 |
Correct |
156 ms |
492 KB |
Output is correct |
9 |
Correct |
308 ms |
620 KB |
Output is correct |
10 |
Correct |
752 ms |
748 KB |
Output is correct |
11 |
Correct |
747 ms |
636 KB |
Output is correct |
12 |
Correct |
764 ms |
748 KB |
Output is correct |
13 |
Correct |
511 ms |
620 KB |
Output is correct |
14 |
Correct |
1037 ms |
620 KB |
Output is correct |
15 |
Correct |
1029 ms |
616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
384 KB |
Output is correct |
2 |
Correct |
30 ms |
364 KB |
Output is correct |
3 |
Correct |
250 ms |
492 KB |
Output is correct |
4 |
Correct |
603 ms |
620 KB |
Output is correct |
5 |
Correct |
178 ms |
492 KB |
Output is correct |
6 |
Correct |
258 ms |
492 KB |
Output is correct |
7 |
Correct |
480 ms |
492 KB |
Output is correct |
8 |
Correct |
156 ms |
492 KB |
Output is correct |
9 |
Correct |
308 ms |
620 KB |
Output is correct |
10 |
Correct |
752 ms |
748 KB |
Output is correct |
11 |
Correct |
747 ms |
636 KB |
Output is correct |
12 |
Correct |
764 ms |
748 KB |
Output is correct |
13 |
Correct |
511 ms |
620 KB |
Output is correct |
14 |
Correct |
1037 ms |
620 KB |
Output is correct |
15 |
Correct |
1029 ms |
616 KB |
Output is correct |
16 |
Correct |
384 ms |
492 KB |
Output is correct |
17 |
Correct |
472 ms |
620 KB |
Output is correct |
18 |
Correct |
412 ms |
492 KB |
Output is correct |
19 |
Correct |
84 ms |
620 KB |
Output is correct |
20 |
Correct |
300 ms |
620 KB |
Output is correct |
21 |
Correct |
17 ms |
364 KB |
Output is correct |
22 |
Correct |
93 ms |
492 KB |
Output is correct |
23 |
Correct |
187 ms |
492 KB |
Output is correct |
24 |
Correct |
66 ms |
492 KB |
Output is correct |
25 |
Correct |
743 ms |
748 KB |
Output is correct |
26 |
Correct |
737 ms |
748 KB |
Output is correct |
27 |
Correct |
754 ms |
748 KB |
Output is correct |
28 |
Correct |
516 ms |
748 KB |
Output is correct |
29 |
Correct |
1032 ms |
620 KB |
Output is correct |
30 |
Correct |
1032 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
384 KB |
Output is correct |
2 |
Correct |
30 ms |
364 KB |
Output is correct |
3 |
Correct |
250 ms |
492 KB |
Output is correct |
4 |
Correct |
603 ms |
620 KB |
Output is correct |
5 |
Correct |
178 ms |
492 KB |
Output is correct |
6 |
Correct |
258 ms |
492 KB |
Output is correct |
7 |
Correct |
480 ms |
492 KB |
Output is correct |
8 |
Correct |
156 ms |
492 KB |
Output is correct |
9 |
Correct |
308 ms |
620 KB |
Output is correct |
10 |
Correct |
752 ms |
748 KB |
Output is correct |
11 |
Correct |
747 ms |
636 KB |
Output is correct |
12 |
Correct |
764 ms |
748 KB |
Output is correct |
13 |
Correct |
511 ms |
620 KB |
Output is correct |
14 |
Correct |
1037 ms |
620 KB |
Output is correct |
15 |
Correct |
1029 ms |
616 KB |
Output is correct |
16 |
Correct |
637 ms |
608 KB |
Output is correct |
17 |
Correct |
313 ms |
620 KB |
Output is correct |
18 |
Correct |
90 ms |
492 KB |
Output is correct |
19 |
Correct |
23 ms |
364 KB |
Output is correct |
20 |
Correct |
10 ms |
364 KB |
Output is correct |
21 |
Correct |
13 ms |
492 KB |
Output is correct |
22 |
Correct |
290 ms |
620 KB |
Output is correct |
23 |
Correct |
54 ms |
364 KB |
Output is correct |
24 |
Correct |
521 ms |
620 KB |
Output is correct |
25 |
Correct |
753 ms |
748 KB |
Output is correct |
26 |
Correct |
766 ms |
748 KB |
Output is correct |
27 |
Correct |
748 ms |
748 KB |
Output is correct |
28 |
Correct |
516 ms |
748 KB |
Output is correct |
29 |
Correct |
1046 ms |
616 KB |
Output is correct |
30 |
Correct |
1061 ms |
616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
36 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
3 ms |
364 KB |
Output is correct |
9 |
Correct |
20 ms |
492 KB |
Output is correct |
10 |
Correct |
43 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
10 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
4 ms |
364 KB |
Output is correct |
16 |
Correct |
21 ms |
384 KB |
Output is correct |
17 |
Correct |
21 ms |
364 KB |
Output is correct |
18 |
Correct |
32 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
364 KB |
Output is correct |
20 |
Correct |
42 ms |
364 KB |
Output is correct |
21 |
Correct |
77 ms |
384 KB |
Output is correct |
22 |
Correct |
30 ms |
364 KB |
Output is correct |
23 |
Correct |
250 ms |
492 KB |
Output is correct |
24 |
Correct |
603 ms |
620 KB |
Output is correct |
25 |
Correct |
178 ms |
492 KB |
Output is correct |
26 |
Correct |
258 ms |
492 KB |
Output is correct |
27 |
Correct |
480 ms |
492 KB |
Output is correct |
28 |
Correct |
156 ms |
492 KB |
Output is correct |
29 |
Correct |
308 ms |
620 KB |
Output is correct |
30 |
Correct |
752 ms |
748 KB |
Output is correct |
31 |
Correct |
747 ms |
636 KB |
Output is correct |
32 |
Correct |
764 ms |
748 KB |
Output is correct |
33 |
Correct |
511 ms |
620 KB |
Output is correct |
34 |
Correct |
1037 ms |
620 KB |
Output is correct |
35 |
Correct |
1029 ms |
616 KB |
Output is correct |
36 |
Correct |
384 ms |
492 KB |
Output is correct |
37 |
Correct |
472 ms |
620 KB |
Output is correct |
38 |
Correct |
412 ms |
492 KB |
Output is correct |
39 |
Correct |
84 ms |
620 KB |
Output is correct |
40 |
Correct |
300 ms |
620 KB |
Output is correct |
41 |
Correct |
17 ms |
364 KB |
Output is correct |
42 |
Correct |
93 ms |
492 KB |
Output is correct |
43 |
Correct |
187 ms |
492 KB |
Output is correct |
44 |
Correct |
66 ms |
492 KB |
Output is correct |
45 |
Correct |
743 ms |
748 KB |
Output is correct |
46 |
Correct |
737 ms |
748 KB |
Output is correct |
47 |
Correct |
754 ms |
748 KB |
Output is correct |
48 |
Correct |
516 ms |
748 KB |
Output is correct |
49 |
Correct |
1032 ms |
620 KB |
Output is correct |
50 |
Correct |
1032 ms |
620 KB |
Output is correct |
51 |
Correct |
637 ms |
608 KB |
Output is correct |
52 |
Correct |
313 ms |
620 KB |
Output is correct |
53 |
Correct |
90 ms |
492 KB |
Output is correct |
54 |
Correct |
23 ms |
364 KB |
Output is correct |
55 |
Correct |
10 ms |
364 KB |
Output is correct |
56 |
Correct |
13 ms |
492 KB |
Output is correct |
57 |
Correct |
290 ms |
620 KB |
Output is correct |
58 |
Correct |
54 ms |
364 KB |
Output is correct |
59 |
Correct |
521 ms |
620 KB |
Output is correct |
60 |
Correct |
753 ms |
748 KB |
Output is correct |
61 |
Correct |
766 ms |
748 KB |
Output is correct |
62 |
Correct |
748 ms |
748 KB |
Output is correct |
63 |
Correct |
516 ms |
748 KB |
Output is correct |
64 |
Correct |
1046 ms |
616 KB |
Output is correct |
65 |
Correct |
1061 ms |
616 KB |
Output is correct |
66 |
Correct |
1 ms |
364 KB |
Output is correct |
67 |
Correct |
1 ms |
364 KB |
Output is correct |
68 |
Correct |
1 ms |
364 KB |
Output is correct |
69 |
Correct |
1 ms |
364 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Correct |
1 ms |
364 KB |
Output is correct |
73 |
Correct |
1 ms |
364 KB |
Output is correct |
74 |
Correct |
1 ms |
364 KB |
Output is correct |
75 |
Correct |
1 ms |
364 KB |
Output is correct |
76 |
Correct |
1 ms |
364 KB |
Output is correct |
77 |
Correct |
1 ms |
364 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
1 ms |
364 KB |
Output is correct |
80 |
Correct |
1 ms |
364 KB |
Output is correct |
81 |
Correct |
592 ms |
620 KB |
Output is correct |
82 |
Correct |
140 ms |
504 KB |
Output is correct |
83 |
Correct |
263 ms |
576 KB |
Output is correct |
84 |
Correct |
142 ms |
492 KB |
Output is correct |
85 |
Correct |
234 ms |
620 KB |
Output is correct |
86 |
Correct |
201 ms |
620 KB |
Output is correct |
87 |
Correct |
34 ms |
364 KB |
Output is correct |
88 |
Correct |
22 ms |
364 KB |
Output is correct |
89 |
Correct |
85 ms |
492 KB |
Output is correct |
90 |
Correct |
856 ms |
620 KB |
Output is correct |
91 |
Correct |
731 ms |
620 KB |
Output is correct |
92 |
Correct |
745 ms |
748 KB |
Output is correct |
93 |
Correct |
745 ms |
748 KB |
Output is correct |
94 |
Correct |
754 ms |
620 KB |
Output is correct |
95 |
Correct |
513 ms |
620 KB |
Output is correct |
96 |
Correct |
752 ms |
748 KB |
Output is correct |
97 |
Correct |
745 ms |
620 KB |
Output is correct |
98 |
Correct |
1038 ms |
616 KB |
Output is correct |
99 |
Correct |
1051 ms |
616 KB |
Output is correct |
100 |
Correct |
1036 ms |
620 KB |
Output is correct |