#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 5e3 + 7;
const int Mod = 1e9 + 7;
const int szBL = 240;
const int INF = 2e9;
const int BASE = 137;
int n, K, m, A, B;
int dp[N][N], f[N][N];
int V[N];
int T[N], S[N];
int lst[N][N];
inline void maximize (int &A, int B) { if (A < B) A = B; }
void solution () {
cin >> K >> n >> m >> A >> B;
rep (i, 1, K) cin >> V[i];
rep (i, 1, n) cin >> S[i];
rep (i, 1, m) cin >> T[i];
rep (i, 1, K)
rep (j, 1, n) {
lst[i][j] = (S[j] == i) ? j : lst[i][j - 1];
}
memset(dp, -0x3f, sizeof dp);
memset(f, -0x3f, sizeof f);
rep (j, 1, n) {
rep (i, 1, m) {
if (S[j] == T[i]) {
if (i == 1) maximize(dp[i][j], V[T[1]]);
else maximize (dp[i][j], A + B * (i - 1) + V[T[i]]);
maximize (dp[i][j], f[i - 1][j - 1] + 2 * A + B * i + B * j + V[T[i]]);
rep (k, 1, i - 1) {
if (S[j - 1] == T[k]) {
dp[i][j] = max(dp[i][j],
dp[k][j - 1] + A * (i - k >= 2) + B * (i - k - 1) + V[T[i]]);
}
}
rep (k, 1, j - 1) {
if (S[k] == T[i - 1]) {
dp[i][j] = max(dp[i][j],
dp[i - 1][k] + A * (j - k >= 2) + B * (j - k - 1) + V[T[i]]);
}
}
}
f[i][j] = max({f[i - 1][j], f[i][j - 1], dp[i][j] - B * (j + 1) - B * (i + 1)});
}
}
int res = -INF;
rep (i, 1, m)
rep (j, 1, n) res = max(res, dp[i][j] + A * (i != m) + B * (m - i));
cout << max(res, A + B * m)<<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
/*
*/
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
no bug challenge +2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
198068 KB |
Output is correct |
2 |
Correct |
875 ms |
198072 KB |
Output is correct |
3 |
Correct |
72 ms |
198076 KB |
Output is correct |
4 |
Correct |
69 ms |
198252 KB |
Output is correct |
5 |
Correct |
67 ms |
198224 KB |
Output is correct |
6 |
Correct |
207 ms |
198228 KB |
Output is correct |
7 |
Correct |
65 ms |
198192 KB |
Output is correct |
8 |
Correct |
88 ms |
198228 KB |
Output is correct |
9 |
Correct |
442 ms |
198480 KB |
Output is correct |
10 |
Correct |
1248 ms |
198224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
198228 KB |
Output is correct |
2 |
Correct |
72 ms |
198020 KB |
Output is correct |
3 |
Correct |
205 ms |
198248 KB |
Output is correct |
4 |
Correct |
67 ms |
198136 KB |
Output is correct |
5 |
Correct |
92 ms |
198224 KB |
Output is correct |
6 |
Correct |
443 ms |
198224 KB |
Output is correct |
7 |
Correct |
458 ms |
198048 KB |
Output is correct |
8 |
Correct |
705 ms |
198248 KB |
Output is correct |
9 |
Correct |
96 ms |
198012 KB |
Output is correct |
10 |
Correct |
1195 ms |
198404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
203592 KB |
Output is correct |
2 |
Correct |
89 ms |
209660 KB |
Output is correct |
3 |
Correct |
378 ms |
200300 KB |
Output is correct |
4 |
Correct |
409 ms |
210512 KB |
Output is correct |
5 |
Correct |
451 ms |
198484 KB |
Output is correct |
6 |
Correct |
274 ms |
202324 KB |
Output is correct |
7 |
Correct |
329 ms |
208608 KB |
Output is correct |
8 |
Correct |
128 ms |
216536 KB |
Output is correct |
9 |
Correct |
206 ms |
215480 KB |
Output is correct |
10 |
Correct |
704 ms |
204372 KB |
Output is correct |
11 |
Correct |
586 ms |
206460 KB |
Output is correct |
12 |
Correct |
470 ms |
212816 KB |
Output is correct |
13 |
Correct |
303 ms |
216660 KB |
Output is correct |
14 |
Execution timed out |
2043 ms |
198224 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
203592 KB |
Output is correct |
2 |
Correct |
89 ms |
209660 KB |
Output is correct |
3 |
Correct |
378 ms |
200300 KB |
Output is correct |
4 |
Correct |
409 ms |
210512 KB |
Output is correct |
5 |
Correct |
451 ms |
198484 KB |
Output is correct |
6 |
Correct |
274 ms |
202324 KB |
Output is correct |
7 |
Correct |
329 ms |
208608 KB |
Output is correct |
8 |
Correct |
128 ms |
216536 KB |
Output is correct |
9 |
Correct |
206 ms |
215480 KB |
Output is correct |
10 |
Correct |
704 ms |
204372 KB |
Output is correct |
11 |
Correct |
586 ms |
206460 KB |
Output is correct |
12 |
Correct |
470 ms |
212816 KB |
Output is correct |
13 |
Correct |
303 ms |
216660 KB |
Output is correct |
14 |
Execution timed out |
2043 ms |
198224 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
203592 KB |
Output is correct |
2 |
Correct |
89 ms |
209660 KB |
Output is correct |
3 |
Correct |
378 ms |
200300 KB |
Output is correct |
4 |
Correct |
409 ms |
210512 KB |
Output is correct |
5 |
Correct |
451 ms |
198484 KB |
Output is correct |
6 |
Correct |
274 ms |
202324 KB |
Output is correct |
7 |
Correct |
329 ms |
208608 KB |
Output is correct |
8 |
Correct |
128 ms |
216536 KB |
Output is correct |
9 |
Correct |
206 ms |
215480 KB |
Output is correct |
10 |
Correct |
704 ms |
204372 KB |
Output is correct |
11 |
Correct |
586 ms |
206460 KB |
Output is correct |
12 |
Correct |
470 ms |
212816 KB |
Output is correct |
13 |
Correct |
303 ms |
216660 KB |
Output is correct |
14 |
Execution timed out |
2043 ms |
198224 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
198484 KB |
Output is correct |
2 |
Correct |
80 ms |
198228 KB |
Output is correct |
3 |
Correct |
67 ms |
198484 KB |
Output is correct |
4 |
Correct |
66 ms |
198484 KB |
Output is correct |
5 |
Correct |
66 ms |
198480 KB |
Output is correct |
6 |
Correct |
67 ms |
198144 KB |
Output is correct |
7 |
Correct |
67 ms |
198484 KB |
Output is correct |
8 |
Correct |
65 ms |
198332 KB |
Output is correct |
9 |
Correct |
67 ms |
198356 KB |
Output is correct |
10 |
Correct |
67 ms |
198260 KB |
Output is correct |
11 |
Correct |
73 ms |
198484 KB |
Output is correct |
12 |
Correct |
66 ms |
198480 KB |
Output is correct |
13 |
Correct |
67 ms |
198484 KB |
Output is correct |
14 |
Correct |
71 ms |
198228 KB |
Output is correct |
15 |
Correct |
73 ms |
198176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
198068 KB |
Output is correct |
2 |
Correct |
875 ms |
198072 KB |
Output is correct |
3 |
Correct |
72 ms |
198076 KB |
Output is correct |
4 |
Correct |
69 ms |
198252 KB |
Output is correct |
5 |
Correct |
67 ms |
198224 KB |
Output is correct |
6 |
Correct |
207 ms |
198228 KB |
Output is correct |
7 |
Correct |
65 ms |
198192 KB |
Output is correct |
8 |
Correct |
88 ms |
198228 KB |
Output is correct |
9 |
Correct |
442 ms |
198480 KB |
Output is correct |
10 |
Correct |
1248 ms |
198224 KB |
Output is correct |
11 |
Correct |
73 ms |
198228 KB |
Output is correct |
12 |
Correct |
72 ms |
198020 KB |
Output is correct |
13 |
Correct |
205 ms |
198248 KB |
Output is correct |
14 |
Correct |
67 ms |
198136 KB |
Output is correct |
15 |
Correct |
92 ms |
198224 KB |
Output is correct |
16 |
Correct |
443 ms |
198224 KB |
Output is correct |
17 |
Correct |
458 ms |
198048 KB |
Output is correct |
18 |
Correct |
705 ms |
198248 KB |
Output is correct |
19 |
Correct |
96 ms |
198012 KB |
Output is correct |
20 |
Correct |
1195 ms |
198404 KB |
Output is correct |
21 |
Correct |
103 ms |
203592 KB |
Output is correct |
22 |
Correct |
89 ms |
209660 KB |
Output is correct |
23 |
Correct |
378 ms |
200300 KB |
Output is correct |
24 |
Correct |
409 ms |
210512 KB |
Output is correct |
25 |
Correct |
451 ms |
198484 KB |
Output is correct |
26 |
Correct |
274 ms |
202324 KB |
Output is correct |
27 |
Correct |
329 ms |
208608 KB |
Output is correct |
28 |
Correct |
128 ms |
216536 KB |
Output is correct |
29 |
Correct |
206 ms |
215480 KB |
Output is correct |
30 |
Correct |
704 ms |
204372 KB |
Output is correct |
31 |
Correct |
586 ms |
206460 KB |
Output is correct |
32 |
Correct |
470 ms |
212816 KB |
Output is correct |
33 |
Correct |
303 ms |
216660 KB |
Output is correct |
34 |
Execution timed out |
2043 ms |
198224 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |