// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
using db = long double;
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
int main() {
cin.tie(0)->sync_with_stdio(0);
int K, N, M, A, B; cin >> K >> N >> M >> A >> B;
vi H(K); for(auto& x : H) cin >> x;
vi S(N); for(auto& x : S) { cin >> x; --x; }
vi T(M); for(auto& x : T) { cin >> x; --x; }
int dp[2][M+1][2], ndp[2][M+1][2];
memset(dp, 0xc0, sizeof(dp));
int ans = -MOD;
for(int x = 0; x <= N; x++) {
memset(ndp, 0xc0, sizeof(ndp));
// can start on any day
dp[1][0][1] = 0;
for(int y = 0; y <= M; y++) for(int ys = 0; ys <= 1; ys++) for(int xs = 0; xs <= 1; xs++) {
if (dp[ys][y][xs] == -MOD) continue;
if (y == M) ans = max(dp[xs][y][ys], ans);
// go to event
if (x < N && y < M && S[x] == T[y]) ndp[1][y+1][1] = max(dp[xs][y][ys] + H[T[y]], ndp[1][y+1][1]);
// skip next event in S (x)
if (x < N) ndp[0][y][ys] = max(dp[xs][y][ys] + B + xs * A, ndp[0][y][ys]);
// skip next event in T (y)
if (y < M) dp[xs][y+1][0] = max(dp[xs][y][ys] + B + ys * A, dp[xs][y+1][0]);
}
for(int y = 0; y <= M; y++) for(int ys = 0; ys <= 1; ys++) for(int xs = 0; xs <= 1; xs++) {
swap(dp[ys][y][xs], ndp[ys][y][xs]);
}
}
cout << ans << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
20 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
6 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
16 ms |
340 KB |
Output is correct |
10 |
Correct |
24 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
6 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
344 KB |
Output is correct |
7 |
Correct |
12 ms |
340 KB |
Output is correct |
8 |
Correct |
17 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
23 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
340 KB |
Output is correct |
2 |
Correct |
20 ms |
336 KB |
Output is correct |
3 |
Correct |
166 ms |
388 KB |
Output is correct |
4 |
Correct |
399 ms |
472 KB |
Output is correct |
5 |
Correct |
122 ms |
460 KB |
Output is correct |
6 |
Correct |
170 ms |
448 KB |
Output is correct |
7 |
Correct |
324 ms |
468 KB |
Output is correct |
8 |
Correct |
104 ms |
392 KB |
Output is correct |
9 |
Correct |
211 ms |
468 KB |
Output is correct |
10 |
Correct |
505 ms |
468 KB |
Output is correct |
11 |
Correct |
497 ms |
468 KB |
Output is correct |
12 |
Correct |
502 ms |
596 KB |
Output is correct |
13 |
Correct |
507 ms |
468 KB |
Output is correct |
14 |
Correct |
623 ms |
508 KB |
Output is correct |
15 |
Correct |
620 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
340 KB |
Output is correct |
2 |
Correct |
20 ms |
336 KB |
Output is correct |
3 |
Correct |
166 ms |
388 KB |
Output is correct |
4 |
Correct |
399 ms |
472 KB |
Output is correct |
5 |
Correct |
122 ms |
460 KB |
Output is correct |
6 |
Correct |
170 ms |
448 KB |
Output is correct |
7 |
Correct |
324 ms |
468 KB |
Output is correct |
8 |
Correct |
104 ms |
392 KB |
Output is correct |
9 |
Correct |
211 ms |
468 KB |
Output is correct |
10 |
Correct |
505 ms |
468 KB |
Output is correct |
11 |
Correct |
497 ms |
468 KB |
Output is correct |
12 |
Correct |
502 ms |
596 KB |
Output is correct |
13 |
Correct |
507 ms |
468 KB |
Output is correct |
14 |
Correct |
623 ms |
508 KB |
Output is correct |
15 |
Correct |
620 ms |
508 KB |
Output is correct |
16 |
Correct |
270 ms |
468 KB |
Output is correct |
17 |
Correct |
328 ms |
468 KB |
Output is correct |
18 |
Correct |
280 ms |
468 KB |
Output is correct |
19 |
Correct |
57 ms |
424 KB |
Output is correct |
20 |
Correct |
201 ms |
424 KB |
Output is correct |
21 |
Correct |
12 ms |
340 KB |
Output is correct |
22 |
Correct |
64 ms |
440 KB |
Output is correct |
23 |
Correct |
127 ms |
340 KB |
Output is correct |
24 |
Correct |
46 ms |
360 KB |
Output is correct |
25 |
Correct |
512 ms |
532 KB |
Output is correct |
26 |
Correct |
504 ms |
532 KB |
Output is correct |
27 |
Correct |
509 ms |
528 KB |
Output is correct |
28 |
Correct |
518 ms |
524 KB |
Output is correct |
29 |
Correct |
588 ms |
516 KB |
Output is correct |
30 |
Correct |
589 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
340 KB |
Output is correct |
2 |
Correct |
20 ms |
336 KB |
Output is correct |
3 |
Correct |
166 ms |
388 KB |
Output is correct |
4 |
Correct |
399 ms |
472 KB |
Output is correct |
5 |
Correct |
122 ms |
460 KB |
Output is correct |
6 |
Correct |
170 ms |
448 KB |
Output is correct |
7 |
Correct |
324 ms |
468 KB |
Output is correct |
8 |
Correct |
104 ms |
392 KB |
Output is correct |
9 |
Correct |
211 ms |
468 KB |
Output is correct |
10 |
Correct |
505 ms |
468 KB |
Output is correct |
11 |
Correct |
497 ms |
468 KB |
Output is correct |
12 |
Correct |
502 ms |
596 KB |
Output is correct |
13 |
Correct |
507 ms |
468 KB |
Output is correct |
14 |
Correct |
623 ms |
508 KB |
Output is correct |
15 |
Correct |
620 ms |
508 KB |
Output is correct |
16 |
Correct |
424 ms |
492 KB |
Output is correct |
17 |
Correct |
216 ms |
404 KB |
Output is correct |
18 |
Correct |
60 ms |
340 KB |
Output is correct |
19 |
Correct |
15 ms |
340 KB |
Output is correct |
20 |
Correct |
8 ms |
340 KB |
Output is correct |
21 |
Correct |
12 ms |
484 KB |
Output is correct |
22 |
Correct |
213 ms |
468 KB |
Output is correct |
23 |
Correct |
36 ms |
352 KB |
Output is correct |
24 |
Correct |
344 ms |
460 KB |
Output is correct |
25 |
Correct |
497 ms |
536 KB |
Output is correct |
26 |
Correct |
502 ms |
588 KB |
Output is correct |
27 |
Correct |
501 ms |
528 KB |
Output is correct |
28 |
Correct |
497 ms |
516 KB |
Output is correct |
29 |
Correct |
573 ms |
512 KB |
Output is correct |
30 |
Correct |
572 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
20 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
6 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
16 ms |
340 KB |
Output is correct |
10 |
Correct |
24 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
6 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
12 ms |
344 KB |
Output is correct |
17 |
Correct |
12 ms |
340 KB |
Output is correct |
18 |
Correct |
17 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
212 KB |
Output is correct |
20 |
Correct |
23 ms |
340 KB |
Output is correct |
21 |
Correct |
62 ms |
340 KB |
Output is correct |
22 |
Correct |
20 ms |
336 KB |
Output is correct |
23 |
Correct |
166 ms |
388 KB |
Output is correct |
24 |
Correct |
399 ms |
472 KB |
Output is correct |
25 |
Correct |
122 ms |
460 KB |
Output is correct |
26 |
Correct |
170 ms |
448 KB |
Output is correct |
27 |
Correct |
324 ms |
468 KB |
Output is correct |
28 |
Correct |
104 ms |
392 KB |
Output is correct |
29 |
Correct |
211 ms |
468 KB |
Output is correct |
30 |
Correct |
505 ms |
468 KB |
Output is correct |
31 |
Correct |
497 ms |
468 KB |
Output is correct |
32 |
Correct |
502 ms |
596 KB |
Output is correct |
33 |
Correct |
507 ms |
468 KB |
Output is correct |
34 |
Correct |
623 ms |
508 KB |
Output is correct |
35 |
Correct |
620 ms |
508 KB |
Output is correct |
36 |
Correct |
270 ms |
468 KB |
Output is correct |
37 |
Correct |
328 ms |
468 KB |
Output is correct |
38 |
Correct |
280 ms |
468 KB |
Output is correct |
39 |
Correct |
57 ms |
424 KB |
Output is correct |
40 |
Correct |
201 ms |
424 KB |
Output is correct |
41 |
Correct |
12 ms |
340 KB |
Output is correct |
42 |
Correct |
64 ms |
440 KB |
Output is correct |
43 |
Correct |
127 ms |
340 KB |
Output is correct |
44 |
Correct |
46 ms |
360 KB |
Output is correct |
45 |
Correct |
512 ms |
532 KB |
Output is correct |
46 |
Correct |
504 ms |
532 KB |
Output is correct |
47 |
Correct |
509 ms |
528 KB |
Output is correct |
48 |
Correct |
518 ms |
524 KB |
Output is correct |
49 |
Correct |
588 ms |
516 KB |
Output is correct |
50 |
Correct |
589 ms |
512 KB |
Output is correct |
51 |
Correct |
424 ms |
492 KB |
Output is correct |
52 |
Correct |
216 ms |
404 KB |
Output is correct |
53 |
Correct |
60 ms |
340 KB |
Output is correct |
54 |
Correct |
15 ms |
340 KB |
Output is correct |
55 |
Correct |
8 ms |
340 KB |
Output is correct |
56 |
Correct |
12 ms |
484 KB |
Output is correct |
57 |
Correct |
213 ms |
468 KB |
Output is correct |
58 |
Correct |
36 ms |
352 KB |
Output is correct |
59 |
Correct |
344 ms |
460 KB |
Output is correct |
60 |
Correct |
497 ms |
536 KB |
Output is correct |
61 |
Correct |
502 ms |
588 KB |
Output is correct |
62 |
Correct |
501 ms |
528 KB |
Output is correct |
63 |
Correct |
497 ms |
516 KB |
Output is correct |
64 |
Correct |
573 ms |
512 KB |
Output is correct |
65 |
Correct |
572 ms |
460 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
0 ms |
212 KB |
Output is correct |
68 |
Correct |
1 ms |
212 KB |
Output is correct |
69 |
Correct |
0 ms |
212 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
212 KB |
Output is correct |
81 |
Correct |
405 ms |
504 KB |
Output is correct |
82 |
Correct |
100 ms |
388 KB |
Output is correct |
83 |
Correct |
177 ms |
468 KB |
Output is correct |
84 |
Correct |
95 ms |
440 KB |
Output is correct |
85 |
Correct |
161 ms |
432 KB |
Output is correct |
86 |
Correct |
137 ms |
412 KB |
Output is correct |
87 |
Correct |
23 ms |
384 KB |
Output is correct |
88 |
Correct |
15 ms |
340 KB |
Output is correct |
89 |
Correct |
56 ms |
380 KB |
Output is correct |
90 |
Correct |
540 ms |
508 KB |
Output is correct |
91 |
Correct |
501 ms |
532 KB |
Output is correct |
92 |
Correct |
550 ms |
528 KB |
Output is correct |
93 |
Correct |
512 ms |
540 KB |
Output is correct |
94 |
Correct |
508 ms |
528 KB |
Output is correct |
95 |
Correct |
516 ms |
516 KB |
Output is correct |
96 |
Correct |
513 ms |
528 KB |
Output is correct |
97 |
Correct |
504 ms |
524 KB |
Output is correct |
98 |
Correct |
589 ms |
468 KB |
Output is correct |
99 |
Correct |
584 ms |
516 KB |
Output is correct |
100 |
Correct |
574 ms |
516 KB |
Output is correct |