#include <bits/stdc++.h>
using namespace std;
using dp_type = array<array<int, 2>, 2>;
const int INF = 1e9;
const dp_type WORST = dp_type{array<int, 2>{-INF, -INF}, array<int, 2>{-INF, -INF}};
void mxm(int &x, int y) {
if (x < y) x = y;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int K, N, M, A, B; cin >> K >> N >> M >> A >> B;
vector<int> V(K), S(N), T(M);
for (int i = 0; i < K; ++i) cin >> V[i];
for (int i = 0; i < N; ++i) cin >> S[i], --S[i];
for (int i = 0; i < M; ++i) cin >> T[i], --T[i];
int ans = A + B*M;
vector<dp_type> dp(M + 1, WORST);
for (int i = 0; i <= N; ++i) {
vector<dp_type> ndp(M + 1, WORST);
for (int j = 0; j <= M; ++j) {
// start
if (i < N && j < M && S[i] == T[j]) mxm(ndp[j + 1][1][1], V[S[i]] + (j ? A : 0) + j * B);
for (int ld = 0; ld < 2; ++ld) {
for (int le = 0; le < 2; ++le) {
if (dp[j][ld][le] == -INF) continue;
mxm(ans, dp[j][ld][le] + (M - j ? A : 0) + (M - j) * B);
// ignore day
if (i < N) mxm(ndp[j][0][le], dp[j][ld][le] + B + (ld ? A : 0));
// ignore event
if (j < M) mxm(dp[j + 1][ld][0], dp[j][ld][le] + B + (le ? A : 0));
// take
if (i < N && j < M && S[i] == T[j]) {
mxm(ndp[j + 1][1][1], dp[j][ld][le] + V[S[i]]);
}
}
}
}
if (i < N) dp = move(ndp);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
27 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
8 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
16 ms |
384 KB |
Output is correct |
10 |
Correct |
33 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
8 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
324 KB |
Output is correct |
6 |
Correct |
16 ms |
384 KB |
Output is correct |
7 |
Correct |
18 ms |
384 KB |
Output is correct |
8 |
Correct |
23 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
32 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
384 KB |
Output is correct |
2 |
Correct |
21 ms |
384 KB |
Output is correct |
3 |
Correct |
168 ms |
384 KB |
Output is correct |
4 |
Correct |
374 ms |
512 KB |
Output is correct |
5 |
Correct |
137 ms |
632 KB |
Output is correct |
6 |
Correct |
169 ms |
632 KB |
Output is correct |
7 |
Correct |
294 ms |
512 KB |
Output is correct |
8 |
Correct |
94 ms |
504 KB |
Output is correct |
9 |
Correct |
193 ms |
556 KB |
Output is correct |
10 |
Correct |
464 ms |
616 KB |
Output is correct |
11 |
Correct |
468 ms |
512 KB |
Output is correct |
12 |
Correct |
474 ms |
552 KB |
Output is correct |
13 |
Correct |
273 ms |
604 KB |
Output is correct |
14 |
Correct |
729 ms |
532 KB |
Output is correct |
15 |
Correct |
728 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
384 KB |
Output is correct |
2 |
Correct |
21 ms |
384 KB |
Output is correct |
3 |
Correct |
168 ms |
384 KB |
Output is correct |
4 |
Correct |
374 ms |
512 KB |
Output is correct |
5 |
Correct |
137 ms |
632 KB |
Output is correct |
6 |
Correct |
169 ms |
632 KB |
Output is correct |
7 |
Correct |
294 ms |
512 KB |
Output is correct |
8 |
Correct |
94 ms |
504 KB |
Output is correct |
9 |
Correct |
193 ms |
556 KB |
Output is correct |
10 |
Correct |
464 ms |
616 KB |
Output is correct |
11 |
Correct |
468 ms |
512 KB |
Output is correct |
12 |
Correct |
474 ms |
552 KB |
Output is correct |
13 |
Correct |
273 ms |
604 KB |
Output is correct |
14 |
Correct |
729 ms |
532 KB |
Output is correct |
15 |
Correct |
728 ms |
640 KB |
Output is correct |
16 |
Correct |
246 ms |
632 KB |
Output is correct |
17 |
Correct |
301 ms |
632 KB |
Output is correct |
18 |
Correct |
267 ms |
632 KB |
Output is correct |
19 |
Correct |
59 ms |
504 KB |
Output is correct |
20 |
Correct |
201 ms |
540 KB |
Output is correct |
21 |
Correct |
11 ms |
384 KB |
Output is correct |
22 |
Correct |
62 ms |
512 KB |
Output is correct |
23 |
Correct |
125 ms |
512 KB |
Output is correct |
24 |
Correct |
45 ms |
504 KB |
Output is correct |
25 |
Correct |
480 ms |
620 KB |
Output is correct |
26 |
Correct |
505 ms |
632 KB |
Output is correct |
27 |
Correct |
493 ms |
512 KB |
Output is correct |
28 |
Correct |
269 ms |
536 KB |
Output is correct |
29 |
Correct |
789 ms |
600 KB |
Output is correct |
30 |
Correct |
796 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
384 KB |
Output is correct |
2 |
Correct |
21 ms |
384 KB |
Output is correct |
3 |
Correct |
168 ms |
384 KB |
Output is correct |
4 |
Correct |
374 ms |
512 KB |
Output is correct |
5 |
Correct |
137 ms |
632 KB |
Output is correct |
6 |
Correct |
169 ms |
632 KB |
Output is correct |
7 |
Correct |
294 ms |
512 KB |
Output is correct |
8 |
Correct |
94 ms |
504 KB |
Output is correct |
9 |
Correct |
193 ms |
556 KB |
Output is correct |
10 |
Correct |
464 ms |
616 KB |
Output is correct |
11 |
Correct |
468 ms |
512 KB |
Output is correct |
12 |
Correct |
474 ms |
552 KB |
Output is correct |
13 |
Correct |
273 ms |
604 KB |
Output is correct |
14 |
Correct |
729 ms |
532 KB |
Output is correct |
15 |
Correct |
728 ms |
640 KB |
Output is correct |
16 |
Correct |
423 ms |
512 KB |
Output is correct |
17 |
Correct |
202 ms |
384 KB |
Output is correct |
18 |
Correct |
61 ms |
384 KB |
Output is correct |
19 |
Correct |
14 ms |
384 KB |
Output is correct |
20 |
Correct |
7 ms |
384 KB |
Output is correct |
21 |
Correct |
9 ms |
512 KB |
Output is correct |
22 |
Correct |
186 ms |
512 KB |
Output is correct |
23 |
Correct |
41 ms |
512 KB |
Output is correct |
24 |
Correct |
320 ms |
548 KB |
Output is correct |
25 |
Correct |
473 ms |
624 KB |
Output is correct |
26 |
Correct |
489 ms |
632 KB |
Output is correct |
27 |
Correct |
488 ms |
512 KB |
Output is correct |
28 |
Correct |
258 ms |
632 KB |
Output is correct |
29 |
Correct |
751 ms |
632 KB |
Output is correct |
30 |
Correct |
753 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
27 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
8 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
16 ms |
384 KB |
Output is correct |
10 |
Correct |
33 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
8 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
324 KB |
Output is correct |
16 |
Correct |
16 ms |
384 KB |
Output is correct |
17 |
Correct |
18 ms |
384 KB |
Output is correct |
18 |
Correct |
23 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
32 ms |
384 KB |
Output is correct |
21 |
Correct |
46 ms |
384 KB |
Output is correct |
22 |
Correct |
21 ms |
384 KB |
Output is correct |
23 |
Correct |
168 ms |
384 KB |
Output is correct |
24 |
Correct |
374 ms |
512 KB |
Output is correct |
25 |
Correct |
137 ms |
632 KB |
Output is correct |
26 |
Correct |
169 ms |
632 KB |
Output is correct |
27 |
Correct |
294 ms |
512 KB |
Output is correct |
28 |
Correct |
94 ms |
504 KB |
Output is correct |
29 |
Correct |
193 ms |
556 KB |
Output is correct |
30 |
Correct |
464 ms |
616 KB |
Output is correct |
31 |
Correct |
468 ms |
512 KB |
Output is correct |
32 |
Correct |
474 ms |
552 KB |
Output is correct |
33 |
Correct |
273 ms |
604 KB |
Output is correct |
34 |
Correct |
729 ms |
532 KB |
Output is correct |
35 |
Correct |
728 ms |
640 KB |
Output is correct |
36 |
Correct |
246 ms |
632 KB |
Output is correct |
37 |
Correct |
301 ms |
632 KB |
Output is correct |
38 |
Correct |
267 ms |
632 KB |
Output is correct |
39 |
Correct |
59 ms |
504 KB |
Output is correct |
40 |
Correct |
201 ms |
540 KB |
Output is correct |
41 |
Correct |
11 ms |
384 KB |
Output is correct |
42 |
Correct |
62 ms |
512 KB |
Output is correct |
43 |
Correct |
125 ms |
512 KB |
Output is correct |
44 |
Correct |
45 ms |
504 KB |
Output is correct |
45 |
Correct |
480 ms |
620 KB |
Output is correct |
46 |
Correct |
505 ms |
632 KB |
Output is correct |
47 |
Correct |
493 ms |
512 KB |
Output is correct |
48 |
Correct |
269 ms |
536 KB |
Output is correct |
49 |
Correct |
789 ms |
600 KB |
Output is correct |
50 |
Correct |
796 ms |
632 KB |
Output is correct |
51 |
Correct |
423 ms |
512 KB |
Output is correct |
52 |
Correct |
202 ms |
384 KB |
Output is correct |
53 |
Correct |
61 ms |
384 KB |
Output is correct |
54 |
Correct |
14 ms |
384 KB |
Output is correct |
55 |
Correct |
7 ms |
384 KB |
Output is correct |
56 |
Correct |
9 ms |
512 KB |
Output is correct |
57 |
Correct |
186 ms |
512 KB |
Output is correct |
58 |
Correct |
41 ms |
512 KB |
Output is correct |
59 |
Correct |
320 ms |
548 KB |
Output is correct |
60 |
Correct |
473 ms |
624 KB |
Output is correct |
61 |
Correct |
489 ms |
632 KB |
Output is correct |
62 |
Correct |
488 ms |
512 KB |
Output is correct |
63 |
Correct |
258 ms |
632 KB |
Output is correct |
64 |
Correct |
751 ms |
632 KB |
Output is correct |
65 |
Correct |
753 ms |
632 KB |
Output is correct |
66 |
Correct |
1 ms |
384 KB |
Output is correct |
67 |
Correct |
0 ms |
384 KB |
Output is correct |
68 |
Correct |
0 ms |
384 KB |
Output is correct |
69 |
Correct |
0 ms |
384 KB |
Output is correct |
70 |
Correct |
0 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
0 ms |
384 KB |
Output is correct |
73 |
Correct |
0 ms |
384 KB |
Output is correct |
74 |
Correct |
1 ms |
384 KB |
Output is correct |
75 |
Correct |
1 ms |
384 KB |
Output is correct |
76 |
Correct |
1 ms |
384 KB |
Output is correct |
77 |
Correct |
1 ms |
384 KB |
Output is correct |
78 |
Correct |
1 ms |
384 KB |
Output is correct |
79 |
Correct |
1 ms |
384 KB |
Output is correct |
80 |
Correct |
1 ms |
384 KB |
Output is correct |
81 |
Correct |
382 ms |
632 KB |
Output is correct |
82 |
Correct |
92 ms |
384 KB |
Output is correct |
83 |
Correct |
172 ms |
632 KB |
Output is correct |
84 |
Correct |
96 ms |
632 KB |
Output is correct |
85 |
Correct |
152 ms |
512 KB |
Output is correct |
86 |
Correct |
129 ms |
384 KB |
Output is correct |
87 |
Correct |
23 ms |
384 KB |
Output is correct |
88 |
Correct |
15 ms |
384 KB |
Output is correct |
89 |
Correct |
56 ms |
504 KB |
Output is correct |
90 |
Correct |
590 ms |
632 KB |
Output is correct |
91 |
Correct |
492 ms |
632 KB |
Output is correct |
92 |
Correct |
480 ms |
636 KB |
Output is correct |
93 |
Correct |
496 ms |
512 KB |
Output is correct |
94 |
Correct |
492 ms |
632 KB |
Output is correct |
95 |
Correct |
301 ms |
512 KB |
Output is correct |
96 |
Correct |
494 ms |
632 KB |
Output is correct |
97 |
Correct |
494 ms |
544 KB |
Output is correct |
98 |
Correct |
805 ms |
600 KB |
Output is correct |
99 |
Correct |
802 ms |
640 KB |
Output is correct |
100 |
Correct |
791 ms |
528 KB |
Output is correct |