#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 = 1e9;
const int BASE = 137;
int n, K, m, A, B;
int dp[N][N], f[N][N], pt[N][2], ps[N][2];
int V[N];
int T[N], S[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];
memset(dp, -0x3f, sizeof dp);
memset(f, -0x3f, sizeof f);
rep (i, 0, m) pt[i][0] = ps[i][0] = -INF;
rep (j, 1, n) {
rep (i, 0, m) ps[i][j & 1] = -INF;
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]]);
if (T[i - 1] == S[j - 1]) maximize(dp[i][j], dp[i - 1][j - 1] + V[T[i]]);
maximize (dp[i][j], f[i - 1][j - 1] + 2 * A + B * i + B * j + V[T[i]]);
maximize (dp[i][j], ps[i - 1][j & 1 ^ 1] + A + B * i + V[T[i]]);
maximize (dp[i][j], pt[i - 1][j & 1 ^ 1] + A + B * j + V[T[i]]);
// cout << i<<" "<<j<<" "<<ps[i][j & 1 ^ 1]<<" "<<dp[i][j]<<"\n";
// 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)});
pt[i][j & 1] = max(pt[i][j & 1 ^ 1], dp[i][j] - B * (j + 1));
ps[i][j & 1] = max(ps[i - 1][j & 1], dp[i][j] - 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
*/
Compilation message
VisitingSingapore.cpp: In function 'void solution()':
VisitingSingapore.cpp:50:49: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
50 | maximize (dp[i][j], ps[i - 1][j & 1 ^ 1] + A + B * i + V[T[i]]);
| ~~^~~
VisitingSingapore.cpp:51:49: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
51 | maximize (dp[i][j], pt[i - 1][j & 1 ^ 1] + A + B * j + V[T[i]]);
| ~~^~~
VisitingSingapore.cpp:67:40: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
67 | pt[i][j & 1] = max(pt[i][j & 1 ^ 1], dp[i][j] - B * (j + 1));
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
196684 KB |
Output is correct |
2 |
Correct |
96 ms |
196620 KB |
Output is correct |
3 |
Correct |
86 ms |
196596 KB |
Output is correct |
4 |
Correct |
80 ms |
196624 KB |
Output is correct |
5 |
Correct |
83 ms |
196692 KB |
Output is correct |
6 |
Correct |
85 ms |
196688 KB |
Output is correct |
7 |
Correct |
78 ms |
196688 KB |
Output is correct |
8 |
Correct |
85 ms |
196504 KB |
Output is correct |
9 |
Correct |
90 ms |
196472 KB |
Output is correct |
10 |
Correct |
102 ms |
196636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
196692 KB |
Output is correct |
2 |
Correct |
79 ms |
196692 KB |
Output is correct |
3 |
Correct |
83 ms |
196692 KB |
Output is correct |
4 |
Correct |
85 ms |
196476 KB |
Output is correct |
5 |
Correct |
86 ms |
196688 KB |
Output is correct |
6 |
Correct |
90 ms |
196688 KB |
Output is correct |
7 |
Correct |
88 ms |
196692 KB |
Output is correct |
8 |
Correct |
98 ms |
196524 KB |
Output is correct |
9 |
Correct |
86 ms |
196632 KB |
Output is correct |
10 |
Correct |
96 ms |
196732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
196736 KB |
Output is correct |
2 |
Correct |
91 ms |
196692 KB |
Output is correct |
3 |
Correct |
176 ms |
196536 KB |
Output is correct |
4 |
Correct |
310 ms |
196692 KB |
Output is correct |
5 |
Correct |
146 ms |
196776 KB |
Output is correct |
6 |
Correct |
172 ms |
196616 KB |
Output is correct |
7 |
Correct |
268 ms |
196692 KB |
Output is correct |
8 |
Correct |
141 ms |
196688 KB |
Output is correct |
9 |
Correct |
193 ms |
196688 KB |
Output is correct |
10 |
Correct |
360 ms |
196692 KB |
Output is correct |
11 |
Correct |
354 ms |
196688 KB |
Output is correct |
12 |
Correct |
368 ms |
196768 KB |
Output is correct |
13 |
Correct |
361 ms |
196692 KB |
Output is correct |
14 |
Correct |
423 ms |
196944 KB |
Output is correct |
15 |
Correct |
435 ms |
196692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
196736 KB |
Output is correct |
2 |
Correct |
91 ms |
196692 KB |
Output is correct |
3 |
Correct |
176 ms |
196536 KB |
Output is correct |
4 |
Correct |
310 ms |
196692 KB |
Output is correct |
5 |
Correct |
146 ms |
196776 KB |
Output is correct |
6 |
Correct |
172 ms |
196616 KB |
Output is correct |
7 |
Correct |
268 ms |
196692 KB |
Output is correct |
8 |
Correct |
141 ms |
196688 KB |
Output is correct |
9 |
Correct |
193 ms |
196688 KB |
Output is correct |
10 |
Correct |
360 ms |
196692 KB |
Output is correct |
11 |
Correct |
354 ms |
196688 KB |
Output is correct |
12 |
Correct |
368 ms |
196768 KB |
Output is correct |
13 |
Correct |
361 ms |
196692 KB |
Output is correct |
14 |
Correct |
423 ms |
196944 KB |
Output is correct |
15 |
Correct |
435 ms |
196692 KB |
Output is correct |
16 |
Correct |
234 ms |
196576 KB |
Output is correct |
17 |
Correct |
259 ms |
196644 KB |
Output is correct |
18 |
Correct |
242 ms |
196688 KB |
Output is correct |
19 |
Correct |
110 ms |
196688 KB |
Output is correct |
20 |
Correct |
193 ms |
196692 KB |
Output is correct |
21 |
Correct |
87 ms |
196660 KB |
Output is correct |
22 |
Correct |
117 ms |
196652 KB |
Output is correct |
23 |
Correct |
160 ms |
196800 KB |
Output is correct |
24 |
Correct |
109 ms |
196688 KB |
Output is correct |
25 |
Correct |
353 ms |
196712 KB |
Output is correct |
26 |
Correct |
359 ms |
196688 KB |
Output is correct |
27 |
Correct |
361 ms |
196692 KB |
Output is correct |
28 |
Correct |
345 ms |
196688 KB |
Output is correct |
29 |
Correct |
444 ms |
196644 KB |
Output is correct |
30 |
Correct |
418 ms |
196688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
196736 KB |
Output is correct |
2 |
Correct |
91 ms |
196692 KB |
Output is correct |
3 |
Correct |
176 ms |
196536 KB |
Output is correct |
4 |
Correct |
310 ms |
196692 KB |
Output is correct |
5 |
Correct |
146 ms |
196776 KB |
Output is correct |
6 |
Correct |
172 ms |
196616 KB |
Output is correct |
7 |
Correct |
268 ms |
196692 KB |
Output is correct |
8 |
Correct |
141 ms |
196688 KB |
Output is correct |
9 |
Correct |
193 ms |
196688 KB |
Output is correct |
10 |
Correct |
360 ms |
196692 KB |
Output is correct |
11 |
Correct |
354 ms |
196688 KB |
Output is correct |
12 |
Correct |
368 ms |
196768 KB |
Output is correct |
13 |
Correct |
361 ms |
196692 KB |
Output is correct |
14 |
Correct |
423 ms |
196944 KB |
Output is correct |
15 |
Correct |
435 ms |
196692 KB |
Output is correct |
16 |
Correct |
319 ms |
196608 KB |
Output is correct |
17 |
Correct |
183 ms |
196680 KB |
Output is correct |
18 |
Correct |
110 ms |
196592 KB |
Output is correct |
19 |
Correct |
89 ms |
196556 KB |
Output is correct |
20 |
Correct |
87 ms |
196560 KB |
Output is correct |
21 |
Correct |
91 ms |
196688 KB |
Output is correct |
22 |
Correct |
184 ms |
196692 KB |
Output is correct |
23 |
Correct |
105 ms |
196692 KB |
Output is correct |
24 |
Correct |
268 ms |
196688 KB |
Output is correct |
25 |
Correct |
349 ms |
196612 KB |
Output is correct |
26 |
Correct |
368 ms |
196948 KB |
Output is correct |
27 |
Correct |
377 ms |
196688 KB |
Output is correct |
28 |
Correct |
377 ms |
196692 KB |
Output is correct |
29 |
Correct |
424 ms |
196724 KB |
Output is correct |
30 |
Correct |
416 ms |
196816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
196688 KB |
Output is correct |
2 |
Correct |
81 ms |
196632 KB |
Output is correct |
3 |
Correct |
80 ms |
196696 KB |
Output is correct |
4 |
Correct |
81 ms |
196692 KB |
Output is correct |
5 |
Correct |
81 ms |
196688 KB |
Output is correct |
6 |
Correct |
79 ms |
196516 KB |
Output is correct |
7 |
Correct |
80 ms |
196692 KB |
Output is correct |
8 |
Correct |
82 ms |
196468 KB |
Output is correct |
9 |
Correct |
84 ms |
196684 KB |
Output is correct |
10 |
Correct |
81 ms |
196692 KB |
Output is correct |
11 |
Correct |
81 ms |
196688 KB |
Output is correct |
12 |
Correct |
121 ms |
196540 KB |
Output is correct |
13 |
Correct |
81 ms |
196688 KB |
Output is correct |
14 |
Correct |
82 ms |
196476 KB |
Output is correct |
15 |
Correct |
81 ms |
196692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
196684 KB |
Output is correct |
2 |
Correct |
96 ms |
196620 KB |
Output is correct |
3 |
Correct |
86 ms |
196596 KB |
Output is correct |
4 |
Correct |
80 ms |
196624 KB |
Output is correct |
5 |
Correct |
83 ms |
196692 KB |
Output is correct |
6 |
Correct |
85 ms |
196688 KB |
Output is correct |
7 |
Correct |
78 ms |
196688 KB |
Output is correct |
8 |
Correct |
85 ms |
196504 KB |
Output is correct |
9 |
Correct |
90 ms |
196472 KB |
Output is correct |
10 |
Correct |
102 ms |
196636 KB |
Output is correct |
11 |
Correct |
80 ms |
196692 KB |
Output is correct |
12 |
Correct |
79 ms |
196692 KB |
Output is correct |
13 |
Correct |
83 ms |
196692 KB |
Output is correct |
14 |
Correct |
85 ms |
196476 KB |
Output is correct |
15 |
Correct |
86 ms |
196688 KB |
Output is correct |
16 |
Correct |
90 ms |
196688 KB |
Output is correct |
17 |
Correct |
88 ms |
196692 KB |
Output is correct |
18 |
Correct |
98 ms |
196524 KB |
Output is correct |
19 |
Correct |
86 ms |
196632 KB |
Output is correct |
20 |
Correct |
96 ms |
196732 KB |
Output is correct |
21 |
Correct |
105 ms |
196736 KB |
Output is correct |
22 |
Correct |
91 ms |
196692 KB |
Output is correct |
23 |
Correct |
176 ms |
196536 KB |
Output is correct |
24 |
Correct |
310 ms |
196692 KB |
Output is correct |
25 |
Correct |
146 ms |
196776 KB |
Output is correct |
26 |
Correct |
172 ms |
196616 KB |
Output is correct |
27 |
Correct |
268 ms |
196692 KB |
Output is correct |
28 |
Correct |
141 ms |
196688 KB |
Output is correct |
29 |
Correct |
193 ms |
196688 KB |
Output is correct |
30 |
Correct |
360 ms |
196692 KB |
Output is correct |
31 |
Correct |
354 ms |
196688 KB |
Output is correct |
32 |
Correct |
368 ms |
196768 KB |
Output is correct |
33 |
Correct |
361 ms |
196692 KB |
Output is correct |
34 |
Correct |
423 ms |
196944 KB |
Output is correct |
35 |
Correct |
435 ms |
196692 KB |
Output is correct |
36 |
Correct |
234 ms |
196576 KB |
Output is correct |
37 |
Correct |
259 ms |
196644 KB |
Output is correct |
38 |
Correct |
242 ms |
196688 KB |
Output is correct |
39 |
Correct |
110 ms |
196688 KB |
Output is correct |
40 |
Correct |
193 ms |
196692 KB |
Output is correct |
41 |
Correct |
87 ms |
196660 KB |
Output is correct |
42 |
Correct |
117 ms |
196652 KB |
Output is correct |
43 |
Correct |
160 ms |
196800 KB |
Output is correct |
44 |
Correct |
109 ms |
196688 KB |
Output is correct |
45 |
Correct |
353 ms |
196712 KB |
Output is correct |
46 |
Correct |
359 ms |
196688 KB |
Output is correct |
47 |
Correct |
361 ms |
196692 KB |
Output is correct |
48 |
Correct |
345 ms |
196688 KB |
Output is correct |
49 |
Correct |
444 ms |
196644 KB |
Output is correct |
50 |
Correct |
418 ms |
196688 KB |
Output is correct |
51 |
Correct |
319 ms |
196608 KB |
Output is correct |
52 |
Correct |
183 ms |
196680 KB |
Output is correct |
53 |
Correct |
110 ms |
196592 KB |
Output is correct |
54 |
Correct |
89 ms |
196556 KB |
Output is correct |
55 |
Correct |
87 ms |
196560 KB |
Output is correct |
56 |
Correct |
91 ms |
196688 KB |
Output is correct |
57 |
Correct |
184 ms |
196692 KB |
Output is correct |
58 |
Correct |
105 ms |
196692 KB |
Output is correct |
59 |
Correct |
268 ms |
196688 KB |
Output is correct |
60 |
Correct |
349 ms |
196612 KB |
Output is correct |
61 |
Correct |
368 ms |
196948 KB |
Output is correct |
62 |
Correct |
377 ms |
196688 KB |
Output is correct |
63 |
Correct |
377 ms |
196692 KB |
Output is correct |
64 |
Correct |
424 ms |
196724 KB |
Output is correct |
65 |
Correct |
416 ms |
196816 KB |
Output is correct |
66 |
Correct |
77 ms |
196688 KB |
Output is correct |
67 |
Correct |
81 ms |
196632 KB |
Output is correct |
68 |
Correct |
80 ms |
196696 KB |
Output is correct |
69 |
Correct |
81 ms |
196692 KB |
Output is correct |
70 |
Correct |
81 ms |
196688 KB |
Output is correct |
71 |
Correct |
79 ms |
196516 KB |
Output is correct |
72 |
Correct |
80 ms |
196692 KB |
Output is correct |
73 |
Correct |
82 ms |
196468 KB |
Output is correct |
74 |
Correct |
84 ms |
196684 KB |
Output is correct |
75 |
Correct |
81 ms |
196692 KB |
Output is correct |
76 |
Correct |
81 ms |
196688 KB |
Output is correct |
77 |
Correct |
121 ms |
196540 KB |
Output is correct |
78 |
Correct |
81 ms |
196688 KB |
Output is correct |
79 |
Correct |
82 ms |
196476 KB |
Output is correct |
80 |
Correct |
81 ms |
196692 KB |
Output is correct |
81 |
Correct |
296 ms |
196688 KB |
Output is correct |
82 |
Correct |
130 ms |
196688 KB |
Output is correct |
83 |
Correct |
171 ms |
196684 KB |
Output is correct |
84 |
Correct |
141 ms |
196692 KB |
Output is correct |
85 |
Correct |
168 ms |
196688 KB |
Output is correct |
86 |
Correct |
151 ms |
196688 KB |
Output is correct |
87 |
Correct |
94 ms |
196728 KB |
Output is correct |
88 |
Correct |
90 ms |
196688 KB |
Output is correct |
89 |
Correct |
111 ms |
196688 KB |
Output is correct |
90 |
Correct |
343 ms |
196668 KB |
Output is correct |
91 |
Correct |
340 ms |
196692 KB |
Output is correct |
92 |
Correct |
329 ms |
196692 KB |
Output is correct |
93 |
Correct |
350 ms |
196688 KB |
Output is correct |
94 |
Correct |
335 ms |
196696 KB |
Output is correct |
95 |
Correct |
347 ms |
196684 KB |
Output is correct |
96 |
Correct |
339 ms |
196692 KB |
Output is correct |
97 |
Correct |
340 ms |
196948 KB |
Output is correct |
98 |
Correct |
381 ms |
196692 KB |
Output is correct |
99 |
Correct |
396 ms |
196812 KB |
Output is correct |
100 |
Correct |
383 ms |
196620 KB |
Output is correct |