#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[1005][1005][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;
}
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;
}
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:35:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
35 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
31956 KB |
Output is correct |
2 |
Correct |
79 ms |
32124 KB |
Output is correct |
3 |
Correct |
14 ms |
31956 KB |
Output is correct |
4 |
Correct |
14 ms |
31984 KB |
Output is correct |
5 |
Correct |
13 ms |
31956 KB |
Output is correct |
6 |
Correct |
36 ms |
31956 KB |
Output is correct |
7 |
Correct |
15 ms |
31956 KB |
Output is correct |
8 |
Correct |
22 ms |
32016 KB |
Output is correct |
9 |
Correct |
51 ms |
32084 KB |
Output is correct |
10 |
Correct |
95 ms |
32240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
31956 KB |
Output is correct |
2 |
Correct |
14 ms |
31944 KB |
Output is correct |
3 |
Correct |
31 ms |
31956 KB |
Output is correct |
4 |
Correct |
16 ms |
31888 KB |
Output is correct |
5 |
Correct |
18 ms |
31924 KB |
Output is correct |
6 |
Correct |
52 ms |
32084 KB |
Output is correct |
7 |
Correct |
53 ms |
32092 KB |
Output is correct |
8 |
Correct |
67 ms |
32060 KB |
Output is correct |
9 |
Correct |
19 ms |
31916 KB |
Output is correct |
10 |
Correct |
102 ms |
32084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
84 ms |
64972 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
84 ms |
64972 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
84 ms |
64972 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
31952 KB |
Output is correct |
2 |
Correct |
13 ms |
31852 KB |
Output is correct |
3 |
Correct |
18 ms |
31940 KB |
Output is correct |
4 |
Correct |
17 ms |
31956 KB |
Output is correct |
5 |
Correct |
13 ms |
31932 KB |
Output is correct |
6 |
Correct |
14 ms |
31956 KB |
Output is correct |
7 |
Correct |
14 ms |
31956 KB |
Output is correct |
8 |
Correct |
13 ms |
31956 KB |
Output is correct |
9 |
Correct |
14 ms |
31956 KB |
Output is correct |
10 |
Correct |
14 ms |
31940 KB |
Output is correct |
11 |
Correct |
14 ms |
31928 KB |
Output is correct |
12 |
Correct |
14 ms |
31908 KB |
Output is correct |
13 |
Correct |
14 ms |
31948 KB |
Output is correct |
14 |
Correct |
15 ms |
31924 KB |
Output is correct |
15 |
Correct |
17 ms |
31972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
31956 KB |
Output is correct |
2 |
Correct |
79 ms |
32124 KB |
Output is correct |
3 |
Correct |
14 ms |
31956 KB |
Output is correct |
4 |
Correct |
14 ms |
31984 KB |
Output is correct |
5 |
Correct |
13 ms |
31956 KB |
Output is correct |
6 |
Correct |
36 ms |
31956 KB |
Output is correct |
7 |
Correct |
15 ms |
31956 KB |
Output is correct |
8 |
Correct |
22 ms |
32016 KB |
Output is correct |
9 |
Correct |
51 ms |
32084 KB |
Output is correct |
10 |
Correct |
95 ms |
32240 KB |
Output is correct |
11 |
Correct |
14 ms |
31956 KB |
Output is correct |
12 |
Correct |
14 ms |
31944 KB |
Output is correct |
13 |
Correct |
31 ms |
31956 KB |
Output is correct |
14 |
Correct |
16 ms |
31888 KB |
Output is correct |
15 |
Correct |
18 ms |
31924 KB |
Output is correct |
16 |
Correct |
52 ms |
32084 KB |
Output is correct |
17 |
Correct |
53 ms |
32092 KB |
Output is correct |
18 |
Correct |
67 ms |
32060 KB |
Output is correct |
19 |
Correct |
19 ms |
31916 KB |
Output is correct |
20 |
Correct |
102 ms |
32084 KB |
Output is correct |
21 |
Runtime error |
84 ms |
64972 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |