#include "bits/stdc++.h"
using namespace std;
// #define endl '\n'
// #define double long double
// #define int long long
// int MOD = 1000 * 1000 * 1000 + 7;
// int MOD = 998244353;
const int MXN = 5005;
const int MXK = 1005;
int k, n, m, a, b;
int hap[MXK];
int eve[MXN];
int want[MXN];
int dp[2][MXN][2][2];
int best(int a, int b, int c, int d) {
return dp[a % 2][b][c][d];
}
signed main() {
cin >> k >> n >> m >> a >> b;
for (int i=1; i<=k; i++) cin >> hap[i];
for (int i=1; i<=n; i++) cin >> eve[i];
for (int i=1; i<=m; i++) cin >> want[i];
int MX = INT_MIN;
for (int day = n+1; day >= 1; day--) {
int sto = day % 2;
for (int event = m+1; event >= 1; event--) {
for (int last_day_taken=0; last_day_taken<2; last_day_taken++) {
for (int last_eve_taken=0; last_eve_taken<2; last_eve_taken++) {
// my choices are:
// * skip today
// * skip wish
// * leave here
// if todays event is wished, then also:
// * watch today's event
if (event > m) {
dp[sto][event][last_day_taken][last_eve_taken] = 0; // done with trip, watched all events, bye (cant watch any more)
}
else if (day > n) {
// ran out of days
// then i'm done anyway, how many events am i forced to skip?
// if i skipped my last event, there's no surcharge here of A, just Bs
if (last_eve_taken) {
// so i didn't skip last event, so i must pay A + (skip) * B
dp[sto][event][last_day_taken][last_eve_taken] = a + (m - event + 1) * b;
}
else {
// i did skip last event so i must pay (skip) * B
dp[sto][event][last_day_taken][last_eve_taken] = (m - event + 1) * b;
}
}
else {
// i can just leave today and skip the rest
int missing = m - event + 1;
int opt = missing * b + a;
if (last_eve_taken == false) {
opt = missing * b;
}
// do i want to skip today?
if (last_day_taken) {
// last day was taken, so i need a and b to be paid
opt = max(opt, a + b + best(day+1, event, 0, last_eve_taken)); // last day wont be taken anymore
}
else {
// no need a surcharge
opt = max(opt, b + best(day+1, event, 0, last_eve_taken));
}
// do i want to skip this wish?
if (last_eve_taken) {
// last event was taken so i need a and b to be paid
opt = max(opt, a + b + best(day, event+1, last_day_taken, 0));
}
else {
// no need a surcharge
opt = max(opt, b + best(day, event+1, last_day_taken, 0));
}
// can i match this wish?
if (eve[day] == want[event]) {
// i can watch this!
// cout << day << " " << event << endl;
opt = max(opt, hap[want[event]] + best(day+1, event+1, 1, 1)); // all taken, next day, next event
}
dp[sto][event][last_day_taken][last_eve_taken] = opt;
}
}
}
int j = event;
int skipcost = (j - 1) * b + a;
if (j - 1 == 0) skipcost = 0;
int here = dp[sto][j][1][1] + skipcost;
MX = max(MX, here);
// cout << "startday: " << i << " firstev: " << j << " " << here << endl;
}
}
cout << MX << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
23 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
16 ms |
384 KB |
Output is correct |
10 |
Correct |
28 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
10 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
16 ms |
384 KB |
Output is correct |
7 |
Correct |
16 ms |
256 KB |
Output is correct |
8 |
Correct |
22 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
27 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
504 KB |
Output is correct |
2 |
Correct |
29 ms |
416 KB |
Output is correct |
3 |
Correct |
200 ms |
512 KB |
Output is correct |
4 |
Correct |
519 ms |
632 KB |
Output is correct |
5 |
Correct |
143 ms |
512 KB |
Output is correct |
6 |
Correct |
207 ms |
540 KB |
Output is correct |
7 |
Correct |
422 ms |
632 KB |
Output is correct |
8 |
Correct |
130 ms |
384 KB |
Output is correct |
9 |
Correct |
253 ms |
632 KB |
Output is correct |
10 |
Correct |
600 ms |
512 KB |
Output is correct |
11 |
Correct |
607 ms |
632 KB |
Output is correct |
12 |
Correct |
600 ms |
636 KB |
Output is correct |
13 |
Correct |
590 ms |
512 KB |
Output is correct |
14 |
Correct |
569 ms |
632 KB |
Output is correct |
15 |
Correct |
558 ms |
576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
504 KB |
Output is correct |
2 |
Correct |
29 ms |
416 KB |
Output is correct |
3 |
Correct |
200 ms |
512 KB |
Output is correct |
4 |
Correct |
519 ms |
632 KB |
Output is correct |
5 |
Correct |
143 ms |
512 KB |
Output is correct |
6 |
Correct |
207 ms |
540 KB |
Output is correct |
7 |
Correct |
422 ms |
632 KB |
Output is correct |
8 |
Correct |
130 ms |
384 KB |
Output is correct |
9 |
Correct |
253 ms |
632 KB |
Output is correct |
10 |
Correct |
600 ms |
512 KB |
Output is correct |
11 |
Correct |
607 ms |
632 KB |
Output is correct |
12 |
Correct |
600 ms |
636 KB |
Output is correct |
13 |
Correct |
590 ms |
512 KB |
Output is correct |
14 |
Correct |
569 ms |
632 KB |
Output is correct |
15 |
Correct |
558 ms |
576 KB |
Output is correct |
16 |
Correct |
342 ms |
512 KB |
Output is correct |
17 |
Correct |
391 ms |
632 KB |
Output is correct |
18 |
Correct |
358 ms |
512 KB |
Output is correct |
19 |
Correct |
72 ms |
384 KB |
Output is correct |
20 |
Correct |
245 ms |
632 KB |
Output is correct |
21 |
Correct |
18 ms |
384 KB |
Output is correct |
22 |
Correct |
83 ms |
512 KB |
Output is correct |
23 |
Correct |
158 ms |
512 KB |
Output is correct |
24 |
Correct |
60 ms |
504 KB |
Output is correct |
25 |
Correct |
612 ms |
512 KB |
Output is correct |
26 |
Correct |
601 ms |
632 KB |
Output is correct |
27 |
Correct |
607 ms |
632 KB |
Output is correct |
28 |
Correct |
592 ms |
512 KB |
Output is correct |
29 |
Correct |
567 ms |
632 KB |
Output is correct |
30 |
Correct |
575 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
504 KB |
Output is correct |
2 |
Correct |
29 ms |
416 KB |
Output is correct |
3 |
Correct |
200 ms |
512 KB |
Output is correct |
4 |
Correct |
519 ms |
632 KB |
Output is correct |
5 |
Correct |
143 ms |
512 KB |
Output is correct |
6 |
Correct |
207 ms |
540 KB |
Output is correct |
7 |
Correct |
422 ms |
632 KB |
Output is correct |
8 |
Correct |
130 ms |
384 KB |
Output is correct |
9 |
Correct |
253 ms |
632 KB |
Output is correct |
10 |
Correct |
600 ms |
512 KB |
Output is correct |
11 |
Correct |
607 ms |
632 KB |
Output is correct |
12 |
Correct |
600 ms |
636 KB |
Output is correct |
13 |
Correct |
590 ms |
512 KB |
Output is correct |
14 |
Correct |
569 ms |
632 KB |
Output is correct |
15 |
Correct |
558 ms |
576 KB |
Output is correct |
16 |
Correct |
500 ms |
608 KB |
Output is correct |
17 |
Correct |
238 ms |
504 KB |
Output is correct |
18 |
Correct |
75 ms |
384 KB |
Output is correct |
19 |
Correct |
24 ms |
384 KB |
Output is correct |
20 |
Correct |
14 ms |
384 KB |
Output is correct |
21 |
Correct |
19 ms |
512 KB |
Output is correct |
22 |
Correct |
238 ms |
612 KB |
Output is correct |
23 |
Correct |
47 ms |
384 KB |
Output is correct |
24 |
Correct |
405 ms |
512 KB |
Output is correct |
25 |
Correct |
611 ms |
632 KB |
Output is correct |
26 |
Correct |
628 ms |
632 KB |
Output is correct |
27 |
Correct |
595 ms |
632 KB |
Output is correct |
28 |
Correct |
591 ms |
512 KB |
Output is correct |
29 |
Correct |
570 ms |
632 KB |
Output is correct |
30 |
Correct |
561 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
23 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
16 ms |
384 KB |
Output is correct |
10 |
Correct |
28 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
10 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
16 ms |
384 KB |
Output is correct |
17 |
Correct |
16 ms |
256 KB |
Output is correct |
18 |
Correct |
22 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
27 ms |
384 KB |
Output is correct |
21 |
Correct |
79 ms |
504 KB |
Output is correct |
22 |
Correct |
29 ms |
416 KB |
Output is correct |
23 |
Correct |
200 ms |
512 KB |
Output is correct |
24 |
Correct |
519 ms |
632 KB |
Output is correct |
25 |
Correct |
143 ms |
512 KB |
Output is correct |
26 |
Correct |
207 ms |
540 KB |
Output is correct |
27 |
Correct |
422 ms |
632 KB |
Output is correct |
28 |
Correct |
130 ms |
384 KB |
Output is correct |
29 |
Correct |
253 ms |
632 KB |
Output is correct |
30 |
Correct |
600 ms |
512 KB |
Output is correct |
31 |
Correct |
607 ms |
632 KB |
Output is correct |
32 |
Correct |
600 ms |
636 KB |
Output is correct |
33 |
Correct |
590 ms |
512 KB |
Output is correct |
34 |
Correct |
569 ms |
632 KB |
Output is correct |
35 |
Correct |
558 ms |
576 KB |
Output is correct |
36 |
Correct |
342 ms |
512 KB |
Output is correct |
37 |
Correct |
391 ms |
632 KB |
Output is correct |
38 |
Correct |
358 ms |
512 KB |
Output is correct |
39 |
Correct |
72 ms |
384 KB |
Output is correct |
40 |
Correct |
245 ms |
632 KB |
Output is correct |
41 |
Correct |
18 ms |
384 KB |
Output is correct |
42 |
Correct |
83 ms |
512 KB |
Output is correct |
43 |
Correct |
158 ms |
512 KB |
Output is correct |
44 |
Correct |
60 ms |
504 KB |
Output is correct |
45 |
Correct |
612 ms |
512 KB |
Output is correct |
46 |
Correct |
601 ms |
632 KB |
Output is correct |
47 |
Correct |
607 ms |
632 KB |
Output is correct |
48 |
Correct |
592 ms |
512 KB |
Output is correct |
49 |
Correct |
567 ms |
632 KB |
Output is correct |
50 |
Correct |
575 ms |
632 KB |
Output is correct |
51 |
Correct |
500 ms |
608 KB |
Output is correct |
52 |
Correct |
238 ms |
504 KB |
Output is correct |
53 |
Correct |
75 ms |
384 KB |
Output is correct |
54 |
Correct |
24 ms |
384 KB |
Output is correct |
55 |
Correct |
14 ms |
384 KB |
Output is correct |
56 |
Correct |
19 ms |
512 KB |
Output is correct |
57 |
Correct |
238 ms |
612 KB |
Output is correct |
58 |
Correct |
47 ms |
384 KB |
Output is correct |
59 |
Correct |
405 ms |
512 KB |
Output is correct |
60 |
Correct |
611 ms |
632 KB |
Output is correct |
61 |
Correct |
628 ms |
632 KB |
Output is correct |
62 |
Correct |
595 ms |
632 KB |
Output is correct |
63 |
Correct |
591 ms |
512 KB |
Output is correct |
64 |
Correct |
570 ms |
632 KB |
Output is correct |
65 |
Correct |
561 ms |
636 KB |
Output is correct |
66 |
Correct |
5 ms |
384 KB |
Output is correct |
67 |
Correct |
5 ms |
384 KB |
Output is correct |
68 |
Correct |
5 ms |
384 KB |
Output is correct |
69 |
Correct |
5 ms |
384 KB |
Output is correct |
70 |
Correct |
5 ms |
256 KB |
Output is correct |
71 |
Correct |
5 ms |
384 KB |
Output is correct |
72 |
Correct |
5 ms |
384 KB |
Output is correct |
73 |
Correct |
5 ms |
384 KB |
Output is correct |
74 |
Correct |
5 ms |
384 KB |
Output is correct |
75 |
Correct |
6 ms |
384 KB |
Output is correct |
76 |
Correct |
5 ms |
384 KB |
Output is correct |
77 |
Correct |
5 ms |
384 KB |
Output is correct |
78 |
Correct |
6 ms |
384 KB |
Output is correct |
79 |
Correct |
5 ms |
384 KB |
Output is correct |
80 |
Correct |
5 ms |
384 KB |
Output is correct |
81 |
Correct |
489 ms |
632 KB |
Output is correct |
82 |
Correct |
118 ms |
384 KB |
Output is correct |
83 |
Correct |
214 ms |
512 KB |
Output is correct |
84 |
Correct |
120 ms |
512 KB |
Output is correct |
85 |
Correct |
193 ms |
632 KB |
Output is correct |
86 |
Correct |
166 ms |
384 KB |
Output is correct |
87 |
Correct |
34 ms |
384 KB |
Output is correct |
88 |
Correct |
23 ms |
384 KB |
Output is correct |
89 |
Correct |
74 ms |
504 KB |
Output is correct |
90 |
Correct |
644 ms |
640 KB |
Output is correct |
91 |
Correct |
602 ms |
632 KB |
Output is correct |
92 |
Correct |
625 ms |
632 KB |
Output is correct |
93 |
Correct |
598 ms |
632 KB |
Output is correct |
94 |
Correct |
596 ms |
512 KB |
Output is correct |
95 |
Correct |
607 ms |
632 KB |
Output is correct |
96 |
Correct |
611 ms |
632 KB |
Output is correct |
97 |
Correct |
619 ms |
584 KB |
Output is correct |
98 |
Correct |
578 ms |
608 KB |
Output is correct |
99 |
Correct |
561 ms |
632 KB |
Output is correct |
100 |
Correct |
556 ms |
512 KB |
Output is correct |