#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 998244353;
ll binpow(ll a, ll p, int mod = MOD) {
ll res = 1;
while (p) {
if (p & 1) {
(res *= a) %= mod;
}
p >>= 1;
(a *= a) %= mod;
}
return res;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
const int INF = -1e9;
using state = array<array<int, 2>, 2>;
const state INIT = state{array<int, 2>{INF, INF}, array<int, 2>{INF, INF}};
void solve() {
int K, n, m;
int A, B;
cin >> K >> n >> m >> A >> B;
vector<int> v(K + 1);
for (int i = 1; i <= K; i++) {
cin >> v[i];
}
vector<int> s(n + 1);
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
vector<int> t(m + 1);
for (int i = 1; i <= m; i++) {
cin >> t[i];
}
auto chmax = [&](int& cur, int val) {
if (val < INF / 2) return;
cur = max(cur, val);
};
int ans = A + B * m;
vector<state> dp(m + 1, INIT);
for (int i = 1; i <= n; i++) {
vector<state> nxt(m + 1, INIT);
for (int j = 1; j <= m; j++) {
if (s[i] == t[j]) {
chmax(nxt[j][1][1], v[s[i]] + A * (j > 1) + B * (j - 1));
}
for (int f = 0; f < 2; f++) {
for (int g = 0; g < 2; g++) {
chmax(nxt[j][0][g], dp[j][f][g] + A * f + B);
chmax(nxt[j][f][0], nxt[j - 1][f][g] + A * g + B);
if (s[i] == t[j]) {
chmax(nxt[j][1][1], dp[j - 1][f][g] + v[s[i]]);
}
}
}
for (int f = 0; f < 2; f++) {
for (int g = 0; g < 2; g++) {
chmax(ans, nxt[j][f][g] + A * (m - j > 0) + B * (m - j));
}
}
}
dp = move(nxt);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
24 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
4 ms |
204 KB |
Output is correct |
9 |
Correct |
17 ms |
332 KB |
Output is correct |
10 |
Correct |
29 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
9 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
3 ms |
324 KB |
Output is correct |
6 |
Correct |
16 ms |
336 KB |
Output is correct |
7 |
Correct |
17 ms |
348 KB |
Output is correct |
8 |
Correct |
21 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
204 KB |
Output is correct |
10 |
Correct |
29 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
332 KB |
Output is correct |
2 |
Correct |
22 ms |
332 KB |
Output is correct |
3 |
Correct |
206 ms |
388 KB |
Output is correct |
4 |
Correct |
460 ms |
464 KB |
Output is correct |
5 |
Correct |
150 ms |
440 KB |
Output is correct |
6 |
Correct |
188 ms |
332 KB |
Output is correct |
7 |
Correct |
342 ms |
332 KB |
Output is correct |
8 |
Correct |
118 ms |
332 KB |
Output is correct |
9 |
Correct |
230 ms |
332 KB |
Output is correct |
10 |
Correct |
531 ms |
460 KB |
Output is correct |
11 |
Correct |
531 ms |
492 KB |
Output is correct |
12 |
Correct |
548 ms |
580 KB |
Output is correct |
13 |
Correct |
442 ms |
460 KB |
Output is correct |
14 |
Correct |
711 ms |
496 KB |
Output is correct |
15 |
Correct |
708 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
332 KB |
Output is correct |
2 |
Correct |
22 ms |
332 KB |
Output is correct |
3 |
Correct |
206 ms |
388 KB |
Output is correct |
4 |
Correct |
460 ms |
464 KB |
Output is correct |
5 |
Correct |
150 ms |
440 KB |
Output is correct |
6 |
Correct |
188 ms |
332 KB |
Output is correct |
7 |
Correct |
342 ms |
332 KB |
Output is correct |
8 |
Correct |
118 ms |
332 KB |
Output is correct |
9 |
Correct |
230 ms |
332 KB |
Output is correct |
10 |
Correct |
531 ms |
460 KB |
Output is correct |
11 |
Correct |
531 ms |
492 KB |
Output is correct |
12 |
Correct |
548 ms |
580 KB |
Output is correct |
13 |
Correct |
442 ms |
460 KB |
Output is correct |
14 |
Correct |
711 ms |
496 KB |
Output is correct |
15 |
Correct |
708 ms |
488 KB |
Output is correct |
16 |
Correct |
289 ms |
440 KB |
Output is correct |
17 |
Correct |
327 ms |
432 KB |
Output is correct |
18 |
Correct |
301 ms |
452 KB |
Output is correct |
19 |
Correct |
63 ms |
332 KB |
Output is correct |
20 |
Correct |
212 ms |
400 KB |
Output is correct |
21 |
Correct |
12 ms |
332 KB |
Output is correct |
22 |
Correct |
74 ms |
412 KB |
Output is correct |
23 |
Correct |
141 ms |
332 KB |
Output is correct |
24 |
Correct |
47 ms |
332 KB |
Output is correct |
25 |
Correct |
538 ms |
500 KB |
Output is correct |
26 |
Correct |
538 ms |
580 KB |
Output is correct |
27 |
Correct |
551 ms |
460 KB |
Output is correct |
28 |
Correct |
437 ms |
492 KB |
Output is correct |
29 |
Correct |
707 ms |
484 KB |
Output is correct |
30 |
Correct |
697 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
332 KB |
Output is correct |
2 |
Correct |
22 ms |
332 KB |
Output is correct |
3 |
Correct |
206 ms |
388 KB |
Output is correct |
4 |
Correct |
460 ms |
464 KB |
Output is correct |
5 |
Correct |
150 ms |
440 KB |
Output is correct |
6 |
Correct |
188 ms |
332 KB |
Output is correct |
7 |
Correct |
342 ms |
332 KB |
Output is correct |
8 |
Correct |
118 ms |
332 KB |
Output is correct |
9 |
Correct |
230 ms |
332 KB |
Output is correct |
10 |
Correct |
531 ms |
460 KB |
Output is correct |
11 |
Correct |
531 ms |
492 KB |
Output is correct |
12 |
Correct |
548 ms |
580 KB |
Output is correct |
13 |
Correct |
442 ms |
460 KB |
Output is correct |
14 |
Correct |
711 ms |
496 KB |
Output is correct |
15 |
Correct |
708 ms |
488 KB |
Output is correct |
16 |
Correct |
447 ms |
472 KB |
Output is correct |
17 |
Correct |
216 ms |
380 KB |
Output is correct |
18 |
Correct |
66 ms |
392 KB |
Output is correct |
19 |
Correct |
18 ms |
332 KB |
Output is correct |
20 |
Correct |
8 ms |
368 KB |
Output is correct |
21 |
Correct |
12 ms |
456 KB |
Output is correct |
22 |
Correct |
214 ms |
424 KB |
Output is correct |
23 |
Correct |
41 ms |
336 KB |
Output is correct |
24 |
Correct |
366 ms |
452 KB |
Output is correct |
25 |
Correct |
531 ms |
496 KB |
Output is correct |
26 |
Correct |
525 ms |
460 KB |
Output is correct |
27 |
Correct |
538 ms |
580 KB |
Output is correct |
28 |
Correct |
445 ms |
580 KB |
Output is correct |
29 |
Correct |
708 ms |
504 KB |
Output is correct |
30 |
Correct |
705 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
24 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
7 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
4 ms |
204 KB |
Output is correct |
9 |
Correct |
17 ms |
332 KB |
Output is correct |
10 |
Correct |
29 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
9 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
3 ms |
324 KB |
Output is correct |
16 |
Correct |
16 ms |
336 KB |
Output is correct |
17 |
Correct |
17 ms |
348 KB |
Output is correct |
18 |
Correct |
21 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
204 KB |
Output is correct |
20 |
Correct |
29 ms |
352 KB |
Output is correct |
21 |
Correct |
61 ms |
332 KB |
Output is correct |
22 |
Correct |
22 ms |
332 KB |
Output is correct |
23 |
Correct |
206 ms |
388 KB |
Output is correct |
24 |
Correct |
460 ms |
464 KB |
Output is correct |
25 |
Correct |
150 ms |
440 KB |
Output is correct |
26 |
Correct |
188 ms |
332 KB |
Output is correct |
27 |
Correct |
342 ms |
332 KB |
Output is correct |
28 |
Correct |
118 ms |
332 KB |
Output is correct |
29 |
Correct |
230 ms |
332 KB |
Output is correct |
30 |
Correct |
531 ms |
460 KB |
Output is correct |
31 |
Correct |
531 ms |
492 KB |
Output is correct |
32 |
Correct |
548 ms |
580 KB |
Output is correct |
33 |
Correct |
442 ms |
460 KB |
Output is correct |
34 |
Correct |
711 ms |
496 KB |
Output is correct |
35 |
Correct |
708 ms |
488 KB |
Output is correct |
36 |
Correct |
289 ms |
440 KB |
Output is correct |
37 |
Correct |
327 ms |
432 KB |
Output is correct |
38 |
Correct |
301 ms |
452 KB |
Output is correct |
39 |
Correct |
63 ms |
332 KB |
Output is correct |
40 |
Correct |
212 ms |
400 KB |
Output is correct |
41 |
Correct |
12 ms |
332 KB |
Output is correct |
42 |
Correct |
74 ms |
412 KB |
Output is correct |
43 |
Correct |
141 ms |
332 KB |
Output is correct |
44 |
Correct |
47 ms |
332 KB |
Output is correct |
45 |
Correct |
538 ms |
500 KB |
Output is correct |
46 |
Correct |
538 ms |
580 KB |
Output is correct |
47 |
Correct |
551 ms |
460 KB |
Output is correct |
48 |
Correct |
437 ms |
492 KB |
Output is correct |
49 |
Correct |
707 ms |
484 KB |
Output is correct |
50 |
Correct |
697 ms |
492 KB |
Output is correct |
51 |
Correct |
447 ms |
472 KB |
Output is correct |
52 |
Correct |
216 ms |
380 KB |
Output is correct |
53 |
Correct |
66 ms |
392 KB |
Output is correct |
54 |
Correct |
18 ms |
332 KB |
Output is correct |
55 |
Correct |
8 ms |
368 KB |
Output is correct |
56 |
Correct |
12 ms |
456 KB |
Output is correct |
57 |
Correct |
214 ms |
424 KB |
Output is correct |
58 |
Correct |
41 ms |
336 KB |
Output is correct |
59 |
Correct |
366 ms |
452 KB |
Output is correct |
60 |
Correct |
531 ms |
496 KB |
Output is correct |
61 |
Correct |
525 ms |
460 KB |
Output is correct |
62 |
Correct |
538 ms |
580 KB |
Output is correct |
63 |
Correct |
445 ms |
580 KB |
Output is correct |
64 |
Correct |
708 ms |
504 KB |
Output is correct |
65 |
Correct |
705 ms |
488 KB |
Output is correct |
66 |
Correct |
1 ms |
204 KB |
Output is correct |
67 |
Correct |
0 ms |
204 KB |
Output is correct |
68 |
Correct |
0 ms |
204 KB |
Output is correct |
69 |
Correct |
1 ms |
204 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
0 ms |
204 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
308 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
1 ms |
204 KB |
Output is correct |
81 |
Correct |
429 ms |
464 KB |
Output is correct |
82 |
Correct |
101 ms |
364 KB |
Output is correct |
83 |
Correct |
195 ms |
428 KB |
Output is correct |
84 |
Correct |
106 ms |
332 KB |
Output is correct |
85 |
Correct |
177 ms |
408 KB |
Output is correct |
86 |
Correct |
146 ms |
376 KB |
Output is correct |
87 |
Correct |
26 ms |
344 KB |
Output is correct |
88 |
Correct |
16 ms |
332 KB |
Output is correct |
89 |
Correct |
60 ms |
356 KB |
Output is correct |
90 |
Correct |
592 ms |
460 KB |
Output is correct |
91 |
Correct |
530 ms |
500 KB |
Output is correct |
92 |
Correct |
533 ms |
484 KB |
Output is correct |
93 |
Correct |
538 ms |
484 KB |
Output is correct |
94 |
Correct |
547 ms |
488 KB |
Output is correct |
95 |
Correct |
445 ms |
488 KB |
Output is correct |
96 |
Correct |
534 ms |
484 KB |
Output is correct |
97 |
Correct |
544 ms |
504 KB |
Output is correct |
98 |
Correct |
703 ms |
488 KB |
Output is correct |
99 |
Correct |
706 ms |
488 KB |
Output is correct |
100 |
Correct |
702 ms |
460 KB |
Output is correct |