#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define MASK(i) ((1LL) << (i))
#define all(x) x.begin(), x.end()
#define BIT(x, i) ((x) >> (i) & (1LL))
#define dbg(...) cerr << "#" << __LINE__ << ":[" << #__VA_ARGS__ \
<< "] = [" ,DBG(__VA_ARGS__)
string to_string(const string& s) { return '"' + s + '"'; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << to_string(h); if(sizeof...(t)) cerr << ", "; DBG(t...);
}
template <class T>
inline bool maximize(T &a, const T &b) { return (a < b ? (a = b) : 0); }
template <class T>
inline bool minimize(T &a, const T &b) { return (a > b ? (a = b) : 0); }
const int MAXN = 1e5 + 6;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int k, n, m, a, b, v[5005], s[5005], t[5005];
int dp[2][5005][2][2];
void subAC() {
int res = a + m * b;
memset(dp, -0x3f, sizeof (dp));
int inf = dp[0][0][0][0];
for (int i = 0; i <= m; i++) {
dp[0][i][0][1] = a + m * b;
dp[0][i][0][0] = a + m * b;
}
dp[0][0][0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[1][j][0][0] = max({dp[0][j][0][0] + b, dp[1][j - 1][0][0] + b, dp[0][j - 1][1][1] + 2 * b});
dp[1][j][0][1] = max(dp[0][j][0][1], dp[0][j][1][1]) + b;
dp[1][j][1][0] = max(dp[1][j - 1][1][0], dp[1][j - 1][1][1]) + b;
if (s[i] == t[j]) {
dp[1][j][1][1] = v[s[i]] + (j > 1 ? a + (j - 1) * b : 0);
maximize(dp[1][j][1][1], dp[0][j - 1][0][0] + 2 * a + v[s[i]]);
maximize(dp[1][j][1][1], dp[0][j - 1][0][1] + a + v[s[i]]);
maximize(dp[1][j][1][1], dp[0][j - 1][1][0] + a + v[s[i]]);
maximize(dp[1][j][1][1], dp[0][j - 1][1][1] + v[s[i]]);
}
maximize(res, dp[1][j][1][1] + (m - j >= 1 ? a + (m - j) * b : 0));
}
for (int j = 0; j <= m; j++) {
dp[0][j][0][0] = dp[1][j][0][0]; dp[1][j][0][0] = inf;
dp[0][j][0][1] = dp[1][j][0][1]; dp[1][j][0][1] = inf;
dp[0][j][1][0] = dp[1][j][1][0]; dp[1][j][1][0] = inf;
dp[0][j][1][1] = dp[1][j][1][1]; dp[1][j][1][1] = inf;
}
}
cout << res;
}
void solve() {
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];
subAC();
}
#define TASK "FUNTOUR"
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
int ntest = 1;
//cin >> ntest;
while (ntest--) solve();
return 0;
}
//612
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
8 ms |
500 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
5 ms |
496 KB |
Output is correct |
10 |
Correct |
12 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
5 ms |
468 KB |
Output is correct |
8 |
Correct |
7 ms |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
9 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
500 KB |
Output is correct |
3 |
Correct |
64 ms |
468 KB |
Output is correct |
4 |
Correct |
148 ms |
504 KB |
Output is correct |
5 |
Correct |
43 ms |
492 KB |
Output is correct |
6 |
Correct |
62 ms |
488 KB |
Output is correct |
7 |
Correct |
124 ms |
504 KB |
Output is correct |
8 |
Correct |
39 ms |
468 KB |
Output is correct |
9 |
Correct |
76 ms |
588 KB |
Output is correct |
10 |
Correct |
174 ms |
500 KB |
Output is correct |
11 |
Correct |
182 ms |
500 KB |
Output is correct |
12 |
Correct |
182 ms |
504 KB |
Output is correct |
13 |
Correct |
179 ms |
508 KB |
Output is correct |
14 |
Correct |
209 ms |
468 KB |
Output is correct |
15 |
Correct |
200 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
500 KB |
Output is correct |
3 |
Correct |
64 ms |
468 KB |
Output is correct |
4 |
Correct |
148 ms |
504 KB |
Output is correct |
5 |
Correct |
43 ms |
492 KB |
Output is correct |
6 |
Correct |
62 ms |
488 KB |
Output is correct |
7 |
Correct |
124 ms |
504 KB |
Output is correct |
8 |
Correct |
39 ms |
468 KB |
Output is correct |
9 |
Correct |
76 ms |
588 KB |
Output is correct |
10 |
Correct |
174 ms |
500 KB |
Output is correct |
11 |
Correct |
182 ms |
500 KB |
Output is correct |
12 |
Correct |
182 ms |
504 KB |
Output is correct |
13 |
Correct |
179 ms |
508 KB |
Output is correct |
14 |
Correct |
209 ms |
468 KB |
Output is correct |
15 |
Correct |
200 ms |
504 KB |
Output is correct |
16 |
Correct |
94 ms |
468 KB |
Output is correct |
17 |
Correct |
111 ms |
500 KB |
Output is correct |
18 |
Correct |
98 ms |
496 KB |
Output is correct |
19 |
Correct |
21 ms |
468 KB |
Output is correct |
20 |
Correct |
70 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
23 ms |
468 KB |
Output is correct |
23 |
Correct |
45 ms |
496 KB |
Output is correct |
24 |
Correct |
16 ms |
468 KB |
Output is correct |
25 |
Correct |
179 ms |
500 KB |
Output is correct |
26 |
Correct |
174 ms |
504 KB |
Output is correct |
27 |
Correct |
176 ms |
468 KB |
Output is correct |
28 |
Correct |
173 ms |
524 KB |
Output is correct |
29 |
Correct |
202 ms |
468 KB |
Output is correct |
30 |
Correct |
200 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
500 KB |
Output is correct |
3 |
Correct |
64 ms |
468 KB |
Output is correct |
4 |
Correct |
148 ms |
504 KB |
Output is correct |
5 |
Correct |
43 ms |
492 KB |
Output is correct |
6 |
Correct |
62 ms |
488 KB |
Output is correct |
7 |
Correct |
124 ms |
504 KB |
Output is correct |
8 |
Correct |
39 ms |
468 KB |
Output is correct |
9 |
Correct |
76 ms |
588 KB |
Output is correct |
10 |
Correct |
174 ms |
500 KB |
Output is correct |
11 |
Correct |
182 ms |
500 KB |
Output is correct |
12 |
Correct |
182 ms |
504 KB |
Output is correct |
13 |
Correct |
179 ms |
508 KB |
Output is correct |
14 |
Correct |
209 ms |
468 KB |
Output is correct |
15 |
Correct |
200 ms |
504 KB |
Output is correct |
16 |
Correct |
152 ms |
468 KB |
Output is correct |
17 |
Correct |
75 ms |
468 KB |
Output is correct |
18 |
Correct |
21 ms |
520 KB |
Output is correct |
19 |
Correct |
7 ms |
468 KB |
Output is correct |
20 |
Correct |
4 ms |
468 KB |
Output is correct |
21 |
Correct |
6 ms |
532 KB |
Output is correct |
22 |
Correct |
67 ms |
528 KB |
Output is correct |
23 |
Correct |
16 ms |
512 KB |
Output is correct |
24 |
Correct |
138 ms |
524 KB |
Output is correct |
25 |
Correct |
175 ms |
468 KB |
Output is correct |
26 |
Correct |
176 ms |
468 KB |
Output is correct |
27 |
Correct |
173 ms |
544 KB |
Output is correct |
28 |
Correct |
175 ms |
532 KB |
Output is correct |
29 |
Correct |
245 ms |
528 KB |
Output is correct |
30 |
Correct |
202 ms |
520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
480 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
8 ms |
500 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
5 ms |
496 KB |
Output is correct |
10 |
Correct |
12 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
4 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
468 KB |
Output is correct |
18 |
Correct |
7 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
9 ms |
496 KB |
Output is correct |
21 |
Correct |
19 ms |
468 KB |
Output is correct |
22 |
Correct |
10 ms |
500 KB |
Output is correct |
23 |
Correct |
64 ms |
468 KB |
Output is correct |
24 |
Correct |
148 ms |
504 KB |
Output is correct |
25 |
Correct |
43 ms |
492 KB |
Output is correct |
26 |
Correct |
62 ms |
488 KB |
Output is correct |
27 |
Correct |
124 ms |
504 KB |
Output is correct |
28 |
Correct |
39 ms |
468 KB |
Output is correct |
29 |
Correct |
76 ms |
588 KB |
Output is correct |
30 |
Correct |
174 ms |
500 KB |
Output is correct |
31 |
Correct |
182 ms |
500 KB |
Output is correct |
32 |
Correct |
182 ms |
504 KB |
Output is correct |
33 |
Correct |
179 ms |
508 KB |
Output is correct |
34 |
Correct |
209 ms |
468 KB |
Output is correct |
35 |
Correct |
200 ms |
504 KB |
Output is correct |
36 |
Correct |
94 ms |
468 KB |
Output is correct |
37 |
Correct |
111 ms |
500 KB |
Output is correct |
38 |
Correct |
98 ms |
496 KB |
Output is correct |
39 |
Correct |
21 ms |
468 KB |
Output is correct |
40 |
Correct |
70 ms |
468 KB |
Output is correct |
41 |
Correct |
4 ms |
468 KB |
Output is correct |
42 |
Correct |
23 ms |
468 KB |
Output is correct |
43 |
Correct |
45 ms |
496 KB |
Output is correct |
44 |
Correct |
16 ms |
468 KB |
Output is correct |
45 |
Correct |
179 ms |
500 KB |
Output is correct |
46 |
Correct |
174 ms |
504 KB |
Output is correct |
47 |
Correct |
176 ms |
468 KB |
Output is correct |
48 |
Correct |
173 ms |
524 KB |
Output is correct |
49 |
Correct |
202 ms |
468 KB |
Output is correct |
50 |
Correct |
200 ms |
468 KB |
Output is correct |
51 |
Correct |
152 ms |
468 KB |
Output is correct |
52 |
Correct |
75 ms |
468 KB |
Output is correct |
53 |
Correct |
21 ms |
520 KB |
Output is correct |
54 |
Correct |
7 ms |
468 KB |
Output is correct |
55 |
Correct |
4 ms |
468 KB |
Output is correct |
56 |
Correct |
6 ms |
532 KB |
Output is correct |
57 |
Correct |
67 ms |
528 KB |
Output is correct |
58 |
Correct |
16 ms |
512 KB |
Output is correct |
59 |
Correct |
138 ms |
524 KB |
Output is correct |
60 |
Correct |
175 ms |
468 KB |
Output is correct |
61 |
Correct |
176 ms |
468 KB |
Output is correct |
62 |
Correct |
173 ms |
544 KB |
Output is correct |
63 |
Correct |
175 ms |
532 KB |
Output is correct |
64 |
Correct |
245 ms |
528 KB |
Output is correct |
65 |
Correct |
202 ms |
520 KB |
Output is correct |
66 |
Correct |
0 ms |
468 KB |
Output is correct |
67 |
Correct |
0 ms |
468 KB |
Output is correct |
68 |
Correct |
1 ms |
468 KB |
Output is correct |
69 |
Correct |
1 ms |
468 KB |
Output is correct |
70 |
Correct |
1 ms |
468 KB |
Output is correct |
71 |
Correct |
1 ms |
468 KB |
Output is correct |
72 |
Correct |
1 ms |
468 KB |
Output is correct |
73 |
Correct |
1 ms |
468 KB |
Output is correct |
74 |
Correct |
1 ms |
468 KB |
Output is correct |
75 |
Correct |
1 ms |
468 KB |
Output is correct |
76 |
Correct |
1 ms |
480 KB |
Output is correct |
77 |
Correct |
1 ms |
468 KB |
Output is correct |
78 |
Correct |
1 ms |
468 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
0 ms |
468 KB |
Output is correct |
81 |
Correct |
148 ms |
548 KB |
Output is correct |
82 |
Correct |
34 ms |
512 KB |
Output is correct |
83 |
Correct |
64 ms |
524 KB |
Output is correct |
84 |
Correct |
34 ms |
468 KB |
Output is correct |
85 |
Correct |
60 ms |
468 KB |
Output is correct |
86 |
Correct |
48 ms |
468 KB |
Output is correct |
87 |
Correct |
9 ms |
508 KB |
Output is correct |
88 |
Correct |
6 ms |
468 KB |
Output is correct |
89 |
Correct |
20 ms |
516 KB |
Output is correct |
90 |
Correct |
216 ms |
524 KB |
Output is correct |
91 |
Correct |
174 ms |
468 KB |
Output is correct |
92 |
Correct |
172 ms |
536 KB |
Output is correct |
93 |
Correct |
175 ms |
468 KB |
Output is correct |
94 |
Correct |
182 ms |
536 KB |
Output is correct |
95 |
Correct |
170 ms |
520 KB |
Output is correct |
96 |
Correct |
176 ms |
532 KB |
Output is correct |
97 |
Correct |
182 ms |
468 KB |
Output is correct |
98 |
Correct |
201 ms |
520 KB |
Output is correct |
99 |
Correct |
212 ms |
528 KB |
Output is correct |
100 |
Correct |
213 ms |
524 KB |
Output is correct |