#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
int S[5005], T[5005], V[5005], memo[5005][5005][2][2], n, m, k, a, b;
/*
int dp(int x, int y, bool f, bool f2){
if(y == m + 1)return 0;
if(x == n + 1)return b * (m - y + 1) + (!f2 ? a : 0);
if(memo[x][y][f][f2] != -1)return memo[x][y][f][f2];
int res = max(dp(x+1, y, 1, f2) + b + (!f ? a : 0), dp(x, y+1, f, 1) + b + (!f2 ? a : 0));
if(S[x] == T[y])res = max(res, dp(x+1, y+1, 0, 0) + V[S[x]]);
return memo[x][y][f][f2] = res;
}
*/
int dp[5005][5005][2][2];
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];
/*
memset(memo, -1, sizeof(memo));
int res = -1e18;
for(int i=1;i<=n;i++)res = max(res, dp(i, 1, 0, 0));
cout << res;
*/
int res = -1e18;
dp[0][0][0][0] = 0; dp[0][0][1][0] = dp[0][0][1][1] = dp[0][0][0][1] = -1e18;
for(int i=1;i<=m;i++){
dp[0][i][0][0] = dp[0][i][1][0] = dp[0][i][1][1] = -1e18;
dp[0][i][0][1] = a + i * b;
}
for(int i=1;i<=n;i++){
int x = i&1;
dp[x][0][0][0] = 0;
dp[x][0][0][1] = dp[x][0][1][0] = dp[x][0][1][1] = -1e18;
for(int j=1;j<=m;j++){
dp[x][j][0][0] = (S[i] == T[j] ? max({dp[1-x][j-1][0][0], dp[1-x][j-1][0][1], dp[1-x][j-1][1][0], dp[1-x][j-1][1][1]}) + V[S[i]] : (int)-1e18);
dp[x][j][0][1] = max(dp[x][j-1][0][0] + a + b, dp[x][j-1][0][1] + b);
dp[x][j][1][0] = max(dp[1-x][j][0][0] + a + b, dp[1-x][j][1][0] + b);
dp[x][j][1][1] = max({
dp[1-x][j-1][0][0] + a * 2 + b * 2,
dp[1-x][j-1][0][1] + a + b * 2,
dp[1-x][j-1][1][0] + a + b * 2,
dp[1-x][j-1][1][1] + b * 2
});
res = max(res, dp[x][j][0][0] + (m - j) * b + (m == j ? 0 : a));
res = max(res, dp[x][j][1][0] + (m - j) * b + (m == j ? 0 : a));
res = max(res, dp[x][j][0][1] + (m - j) * b);
res = max(res, dp[x][j][1][1] + (m - j) * b);
}
}
cout << res;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
VisitingSingapore.cpp:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
10 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
424 KB |
Output is correct |
8 |
Correct |
7 ms |
428 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
13 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
64 ms |
468 KB |
Output is correct |
4 |
Correct |
155 ms |
696 KB |
Output is correct |
5 |
Correct |
43 ms |
596 KB |
Output is correct |
6 |
Correct |
67 ms |
596 KB |
Output is correct |
7 |
Correct |
116 ms |
620 KB |
Output is correct |
8 |
Correct |
39 ms |
472 KB |
Output is correct |
9 |
Correct |
74 ms |
596 KB |
Output is correct |
10 |
Correct |
185 ms |
724 KB |
Output is correct |
11 |
Correct |
184 ms |
724 KB |
Output is correct |
12 |
Correct |
185 ms |
724 KB |
Output is correct |
13 |
Correct |
196 ms |
728 KB |
Output is correct |
14 |
Correct |
225 ms |
724 KB |
Output is correct |
15 |
Correct |
221 ms |
736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
64 ms |
468 KB |
Output is correct |
4 |
Correct |
155 ms |
696 KB |
Output is correct |
5 |
Correct |
43 ms |
596 KB |
Output is correct |
6 |
Correct |
67 ms |
596 KB |
Output is correct |
7 |
Correct |
116 ms |
620 KB |
Output is correct |
8 |
Correct |
39 ms |
472 KB |
Output is correct |
9 |
Correct |
74 ms |
596 KB |
Output is correct |
10 |
Correct |
185 ms |
724 KB |
Output is correct |
11 |
Correct |
184 ms |
724 KB |
Output is correct |
12 |
Correct |
185 ms |
724 KB |
Output is correct |
13 |
Correct |
196 ms |
728 KB |
Output is correct |
14 |
Correct |
225 ms |
724 KB |
Output is correct |
15 |
Correct |
221 ms |
736 KB |
Output is correct |
16 |
Correct |
96 ms |
596 KB |
Output is correct |
17 |
Correct |
120 ms |
648 KB |
Output is correct |
18 |
Correct |
105 ms |
612 KB |
Output is correct |
19 |
Correct |
25 ms |
472 KB |
Output is correct |
20 |
Correct |
73 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
332 KB |
Output is correct |
22 |
Correct |
24 ms |
516 KB |
Output is correct |
23 |
Correct |
50 ms |
612 KB |
Output is correct |
24 |
Correct |
20 ms |
384 KB |
Output is correct |
25 |
Correct |
180 ms |
772 KB |
Output is correct |
26 |
Correct |
179 ms |
760 KB |
Output is correct |
27 |
Correct |
183 ms |
752 KB |
Output is correct |
28 |
Correct |
211 ms |
760 KB |
Output is correct |
29 |
Correct |
220 ms |
748 KB |
Output is correct |
30 |
Correct |
217 ms |
744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
64 ms |
468 KB |
Output is correct |
4 |
Correct |
155 ms |
696 KB |
Output is correct |
5 |
Correct |
43 ms |
596 KB |
Output is correct |
6 |
Correct |
67 ms |
596 KB |
Output is correct |
7 |
Correct |
116 ms |
620 KB |
Output is correct |
8 |
Correct |
39 ms |
472 KB |
Output is correct |
9 |
Correct |
74 ms |
596 KB |
Output is correct |
10 |
Correct |
185 ms |
724 KB |
Output is correct |
11 |
Correct |
184 ms |
724 KB |
Output is correct |
12 |
Correct |
185 ms |
724 KB |
Output is correct |
13 |
Correct |
196 ms |
728 KB |
Output is correct |
14 |
Correct |
225 ms |
724 KB |
Output is correct |
15 |
Correct |
221 ms |
736 KB |
Output is correct |
16 |
Correct |
156 ms |
680 KB |
Output is correct |
17 |
Correct |
74 ms |
476 KB |
Output is correct |
18 |
Correct |
22 ms |
544 KB |
Output is correct |
19 |
Correct |
6 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
468 KB |
Output is correct |
21 |
Correct |
5 ms |
596 KB |
Output is correct |
22 |
Correct |
69 ms |
596 KB |
Output is correct |
23 |
Correct |
13 ms |
340 KB |
Output is correct |
24 |
Correct |
128 ms |
656 KB |
Output is correct |
25 |
Correct |
187 ms |
768 KB |
Output is correct |
26 |
Correct |
184 ms |
764 KB |
Output is correct |
27 |
Correct |
186 ms |
724 KB |
Output is correct |
28 |
Correct |
188 ms |
748 KB |
Output is correct |
29 |
Correct |
225 ms |
756 KB |
Output is correct |
30 |
Correct |
223 ms |
736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
340 KB |
Output is correct |
10 |
Correct |
10 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
340 KB |
Output is correct |
17 |
Correct |
5 ms |
424 KB |
Output is correct |
18 |
Correct |
7 ms |
428 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
13 ms |
444 KB |
Output is correct |
21 |
Correct |
20 ms |
340 KB |
Output is correct |
22 |
Correct |
8 ms |
340 KB |
Output is correct |
23 |
Correct |
64 ms |
468 KB |
Output is correct |
24 |
Correct |
155 ms |
696 KB |
Output is correct |
25 |
Correct |
43 ms |
596 KB |
Output is correct |
26 |
Correct |
67 ms |
596 KB |
Output is correct |
27 |
Correct |
116 ms |
620 KB |
Output is correct |
28 |
Correct |
39 ms |
472 KB |
Output is correct |
29 |
Correct |
74 ms |
596 KB |
Output is correct |
30 |
Correct |
185 ms |
724 KB |
Output is correct |
31 |
Correct |
184 ms |
724 KB |
Output is correct |
32 |
Correct |
185 ms |
724 KB |
Output is correct |
33 |
Correct |
196 ms |
728 KB |
Output is correct |
34 |
Correct |
225 ms |
724 KB |
Output is correct |
35 |
Correct |
221 ms |
736 KB |
Output is correct |
36 |
Correct |
96 ms |
596 KB |
Output is correct |
37 |
Correct |
120 ms |
648 KB |
Output is correct |
38 |
Correct |
105 ms |
612 KB |
Output is correct |
39 |
Correct |
25 ms |
472 KB |
Output is correct |
40 |
Correct |
73 ms |
468 KB |
Output is correct |
41 |
Correct |
4 ms |
332 KB |
Output is correct |
42 |
Correct |
24 ms |
516 KB |
Output is correct |
43 |
Correct |
50 ms |
612 KB |
Output is correct |
44 |
Correct |
20 ms |
384 KB |
Output is correct |
45 |
Correct |
180 ms |
772 KB |
Output is correct |
46 |
Correct |
179 ms |
760 KB |
Output is correct |
47 |
Correct |
183 ms |
752 KB |
Output is correct |
48 |
Correct |
211 ms |
760 KB |
Output is correct |
49 |
Correct |
220 ms |
748 KB |
Output is correct |
50 |
Correct |
217 ms |
744 KB |
Output is correct |
51 |
Correct |
156 ms |
680 KB |
Output is correct |
52 |
Correct |
74 ms |
476 KB |
Output is correct |
53 |
Correct |
22 ms |
544 KB |
Output is correct |
54 |
Correct |
6 ms |
340 KB |
Output is correct |
55 |
Correct |
4 ms |
468 KB |
Output is correct |
56 |
Correct |
5 ms |
596 KB |
Output is correct |
57 |
Correct |
69 ms |
596 KB |
Output is correct |
58 |
Correct |
13 ms |
340 KB |
Output is correct |
59 |
Correct |
128 ms |
656 KB |
Output is correct |
60 |
Correct |
187 ms |
768 KB |
Output is correct |
61 |
Correct |
184 ms |
764 KB |
Output is correct |
62 |
Correct |
186 ms |
724 KB |
Output is correct |
63 |
Correct |
188 ms |
748 KB |
Output is correct |
64 |
Correct |
225 ms |
756 KB |
Output is correct |
65 |
Correct |
223 ms |
736 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
0 ms |
340 KB |
Output is correct |
68 |
Correct |
0 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
0 ms |
340 KB |
Output is correct |
74 |
Correct |
0 ms |
340 KB |
Output is correct |
75 |
Correct |
0 ms |
340 KB |
Output is correct |
76 |
Correct |
0 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |
81 |
Correct |
159 ms |
724 KB |
Output is correct |
82 |
Correct |
39 ms |
504 KB |
Output is correct |
83 |
Correct |
66 ms |
644 KB |
Output is correct |
84 |
Correct |
36 ms |
596 KB |
Output is correct |
85 |
Correct |
64 ms |
596 KB |
Output is correct |
86 |
Correct |
51 ms |
532 KB |
Output is correct |
87 |
Correct |
9 ms |
468 KB |
Output is correct |
88 |
Correct |
6 ms |
340 KB |
Output is correct |
89 |
Correct |
21 ms |
468 KB |
Output is correct |
90 |
Correct |
205 ms |
740 KB |
Output is correct |
91 |
Correct |
191 ms |
760 KB |
Output is correct |
92 |
Correct |
200 ms |
756 KB |
Output is correct |
93 |
Correct |
183 ms |
724 KB |
Output is correct |
94 |
Correct |
190 ms |
724 KB |
Output is correct |
95 |
Correct |
179 ms |
736 KB |
Output is correct |
96 |
Correct |
190 ms |
752 KB |
Output is correct |
97 |
Correct |
205 ms |
724 KB |
Output is correct |
98 |
Correct |
226 ms |
736 KB |
Output is correct |
99 |
Correct |
229 ms |
740 KB |
Output is correct |
100 |
Correct |
227 ms |
736 KB |
Output is correct |