#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 = 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
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
600 KB |
Output is correct |
10 |
Correct |
8 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
604 KB |
Output is correct |
7 |
Correct |
4 ms |
604 KB |
Output is correct |
8 |
Correct |
5 ms |
636 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
8 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
55 ms |
604 KB |
Output is correct |
4 |
Correct |
132 ms |
600 KB |
Output is correct |
5 |
Correct |
40 ms |
604 KB |
Output is correct |
6 |
Correct |
57 ms |
604 KB |
Output is correct |
7 |
Correct |
105 ms |
604 KB |
Output is correct |
8 |
Correct |
35 ms |
600 KB |
Output is correct |
9 |
Correct |
67 ms |
600 KB |
Output is correct |
10 |
Correct |
165 ms |
676 KB |
Output is correct |
11 |
Correct |
164 ms |
604 KB |
Output is correct |
12 |
Correct |
165 ms |
604 KB |
Output is correct |
13 |
Correct |
167 ms |
604 KB |
Output is correct |
14 |
Correct |
184 ms |
600 KB |
Output is correct |
15 |
Correct |
183 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
55 ms |
604 KB |
Output is correct |
4 |
Correct |
132 ms |
600 KB |
Output is correct |
5 |
Correct |
40 ms |
604 KB |
Output is correct |
6 |
Correct |
57 ms |
604 KB |
Output is correct |
7 |
Correct |
105 ms |
604 KB |
Output is correct |
8 |
Correct |
35 ms |
600 KB |
Output is correct |
9 |
Correct |
67 ms |
600 KB |
Output is correct |
10 |
Correct |
165 ms |
676 KB |
Output is correct |
11 |
Correct |
164 ms |
604 KB |
Output is correct |
12 |
Correct |
165 ms |
604 KB |
Output is correct |
13 |
Correct |
167 ms |
604 KB |
Output is correct |
14 |
Correct |
184 ms |
600 KB |
Output is correct |
15 |
Correct |
183 ms |
856 KB |
Output is correct |
16 |
Correct |
86 ms |
604 KB |
Output is correct |
17 |
Correct |
107 ms |
852 KB |
Output is correct |
18 |
Correct |
92 ms |
604 KB |
Output is correct |
19 |
Correct |
24 ms |
600 KB |
Output is correct |
20 |
Correct |
66 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
21 ms |
664 KB |
Output is correct |
23 |
Correct |
42 ms |
600 KB |
Output is correct |
24 |
Correct |
15 ms |
604 KB |
Output is correct |
25 |
Correct |
164 ms |
600 KB |
Output is correct |
26 |
Correct |
165 ms |
604 KB |
Output is correct |
27 |
Correct |
170 ms |
672 KB |
Output is correct |
28 |
Correct |
173 ms |
668 KB |
Output is correct |
29 |
Correct |
188 ms |
600 KB |
Output is correct |
30 |
Correct |
184 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
55 ms |
604 KB |
Output is correct |
4 |
Correct |
132 ms |
600 KB |
Output is correct |
5 |
Correct |
40 ms |
604 KB |
Output is correct |
6 |
Correct |
57 ms |
604 KB |
Output is correct |
7 |
Correct |
105 ms |
604 KB |
Output is correct |
8 |
Correct |
35 ms |
600 KB |
Output is correct |
9 |
Correct |
67 ms |
600 KB |
Output is correct |
10 |
Correct |
165 ms |
676 KB |
Output is correct |
11 |
Correct |
164 ms |
604 KB |
Output is correct |
12 |
Correct |
165 ms |
604 KB |
Output is correct |
13 |
Correct |
167 ms |
604 KB |
Output is correct |
14 |
Correct |
184 ms |
600 KB |
Output is correct |
15 |
Correct |
183 ms |
856 KB |
Output is correct |
16 |
Correct |
138 ms |
668 KB |
Output is correct |
17 |
Correct |
68 ms |
604 KB |
Output is correct |
18 |
Correct |
21 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
71 ms |
604 KB |
Output is correct |
23 |
Correct |
12 ms |
648 KB |
Output is correct |
24 |
Correct |
118 ms |
604 KB |
Output is correct |
25 |
Correct |
164 ms |
604 KB |
Output is correct |
26 |
Correct |
165 ms |
664 KB |
Output is correct |
27 |
Correct |
171 ms |
680 KB |
Output is correct |
28 |
Correct |
164 ms |
600 KB |
Output is correct |
29 |
Correct |
187 ms |
656 KB |
Output is correct |
30 |
Correct |
186 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
600 KB |
Output is correct |
10 |
Correct |
8 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
604 KB |
Output is correct |
18 |
Correct |
5 ms |
636 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
8 ms |
632 KB |
Output is correct |
21 |
Correct |
17 ms |
604 KB |
Output is correct |
22 |
Correct |
7 ms |
600 KB |
Output is correct |
23 |
Correct |
55 ms |
604 KB |
Output is correct |
24 |
Correct |
132 ms |
600 KB |
Output is correct |
25 |
Correct |
40 ms |
604 KB |
Output is correct |
26 |
Correct |
57 ms |
604 KB |
Output is correct |
27 |
Correct |
105 ms |
604 KB |
Output is correct |
28 |
Correct |
35 ms |
600 KB |
Output is correct |
29 |
Correct |
67 ms |
600 KB |
Output is correct |
30 |
Correct |
165 ms |
676 KB |
Output is correct |
31 |
Correct |
164 ms |
604 KB |
Output is correct |
32 |
Correct |
165 ms |
604 KB |
Output is correct |
33 |
Correct |
167 ms |
604 KB |
Output is correct |
34 |
Correct |
184 ms |
600 KB |
Output is correct |
35 |
Correct |
183 ms |
856 KB |
Output is correct |
36 |
Correct |
86 ms |
604 KB |
Output is correct |
37 |
Correct |
107 ms |
852 KB |
Output is correct |
38 |
Correct |
92 ms |
604 KB |
Output is correct |
39 |
Correct |
24 ms |
600 KB |
Output is correct |
40 |
Correct |
66 ms |
604 KB |
Output is correct |
41 |
Correct |
4 ms |
604 KB |
Output is correct |
42 |
Correct |
21 ms |
664 KB |
Output is correct |
43 |
Correct |
42 ms |
600 KB |
Output is correct |
44 |
Correct |
15 ms |
604 KB |
Output is correct |
45 |
Correct |
164 ms |
600 KB |
Output is correct |
46 |
Correct |
165 ms |
604 KB |
Output is correct |
47 |
Correct |
170 ms |
672 KB |
Output is correct |
48 |
Correct |
173 ms |
668 KB |
Output is correct |
49 |
Correct |
188 ms |
600 KB |
Output is correct |
50 |
Correct |
184 ms |
604 KB |
Output is correct |
51 |
Correct |
138 ms |
668 KB |
Output is correct |
52 |
Correct |
68 ms |
604 KB |
Output is correct |
53 |
Correct |
21 ms |
604 KB |
Output is correct |
54 |
Correct |
5 ms |
604 KB |
Output is correct |
55 |
Correct |
3 ms |
604 KB |
Output is correct |
56 |
Correct |
4 ms |
604 KB |
Output is correct |
57 |
Correct |
71 ms |
604 KB |
Output is correct |
58 |
Correct |
12 ms |
648 KB |
Output is correct |
59 |
Correct |
118 ms |
604 KB |
Output is correct |
60 |
Correct |
164 ms |
604 KB |
Output is correct |
61 |
Correct |
165 ms |
664 KB |
Output is correct |
62 |
Correct |
171 ms |
680 KB |
Output is correct |
63 |
Correct |
164 ms |
600 KB |
Output is correct |
64 |
Correct |
187 ms |
656 KB |
Output is correct |
65 |
Correct |
186 ms |
604 KB |
Output is correct |
66 |
Correct |
1 ms |
604 KB |
Output is correct |
67 |
Correct |
0 ms |
604 KB |
Output is correct |
68 |
Correct |
0 ms |
604 KB |
Output is correct |
69 |
Correct |
1 ms |
604 KB |
Output is correct |
70 |
Correct |
0 ms |
604 KB |
Output is correct |
71 |
Correct |
0 ms |
604 KB |
Output is correct |
72 |
Correct |
0 ms |
600 KB |
Output is correct |
73 |
Correct |
1 ms |
604 KB |
Output is correct |
74 |
Correct |
0 ms |
604 KB |
Output is correct |
75 |
Correct |
1 ms |
604 KB |
Output is correct |
76 |
Correct |
0 ms |
604 KB |
Output is correct |
77 |
Correct |
0 ms |
604 KB |
Output is correct |
78 |
Correct |
1 ms |
604 KB |
Output is correct |
79 |
Correct |
1 ms |
604 KB |
Output is correct |
80 |
Correct |
1 ms |
604 KB |
Output is correct |
81 |
Correct |
132 ms |
604 KB |
Output is correct |
82 |
Correct |
34 ms |
604 KB |
Output is correct |
83 |
Correct |
59 ms |
652 KB |
Output is correct |
84 |
Correct |
33 ms |
604 KB |
Output is correct |
85 |
Correct |
53 ms |
648 KB |
Output is correct |
86 |
Correct |
45 ms |
604 KB |
Output is correct |
87 |
Correct |
8 ms |
604 KB |
Output is correct |
88 |
Correct |
5 ms |
644 KB |
Output is correct |
89 |
Correct |
19 ms |
604 KB |
Output is correct |
90 |
Correct |
197 ms |
848 KB |
Output is correct |
91 |
Correct |
173 ms |
600 KB |
Output is correct |
92 |
Correct |
164 ms |
600 KB |
Output is correct |
93 |
Correct |
165 ms |
680 KB |
Output is correct |
94 |
Correct |
172 ms |
604 KB |
Output is correct |
95 |
Correct |
164 ms |
604 KB |
Output is correct |
96 |
Correct |
165 ms |
600 KB |
Output is correct |
97 |
Correct |
165 ms |
668 KB |
Output is correct |
98 |
Correct |
185 ms |
600 KB |
Output is correct |
99 |
Correct |
186 ms |
604 KB |
Output is correct |
100 |
Correct |
183 ms |
652 KB |
Output is correct |