#include "bits/stdc++.h"
using namespace std;
const int MXK = 1005;
const int MXN = 5005;
const int MXM = 5005;
int k, n, m, a, b;
int happy[MXK];
int events[MXN];
int want[MXM];
int dp[MXN][MXM];
int solve(int planday, int realday) {
if (planday == m) return 0; // if we're at end of plan, no more to gain
if (realday == n) {
// no more days to do anything
int re = (m - planday);
int co = (re == 0) ? 0 : b * re + a;
return co;
}
if (dp[planday][realday] != INT_MIN) return dp[planday][realday];
int best = INT_MIN; // the best is nada
// let's say we skip this day of plan
// there are then m - planday - 1 remaining in plan so
for (int u=planday; u<m; u++) {
// skip until day u of plan
int skip = (u - planday + 1);
int cost = ((skip == 0) ? 0 : b * skip + a);
best = max(best, cost + solve(u+1, realday));
}
// to match this plan day to some other day
for (int i=realday; i<n; i++) {
if (events[i] == want[planday]) {
// we can do this planday on i day
// we'll be skipping i - realday days
int cost = ((i - realday == 0) ? 0 : b * (i - realday) + a);
int gain = happy[events[i]];
// then we'll end up on the day after i
best = max(best, gain + cost + solve(planday+1, i+1));
}
}
return dp[planday][realday] = best;
}
int lcs(int i, int j) {
if (i == n && j == m) {
return 0;
}
if (i == n) {
return lcs(i, j+1) + b;
}
if (j == m) {
return lcs(i+1, j);
}
if (dp[i][j] != INT_MIN) return dp[i][j];
int best = INT_MIN;
if (events[i] == want[j]) {
best = max(best, happy[events[i]] + lcs(i+1, j+1));
}
int g = m-j;
best = max(best, (g==0)?0:g*b);
best = max(best, lcs(i+1, j) + b);
best = max(best, lcs(i, j+1) + b);
return dp[i][j] = best;
}
int main() {
for (int i=0; i<MXN; i++) for (int j=0; j<MXM; j++) dp[i][j] = INT_MIN;
cin >> k >> n >> m >> a >> b;
for (int i=1; i<=k; i++) cin >> happy[i];
for (int i=0; i<n; i++) cin >> events[i];
for (int i=0; i<m; i++) cin >> want[i];
if (k == 1) {
if (n >= m) {
cout << m * happy[1] << endl;
}
else {
cout << n * happy[1] + ((m-n) * b + a) << endl;
}
exit(0);
}
if (a == 0) {
int opt = INT_MIN;
for (int i=0; i<n; i++) opt = max(opt, lcs(i, 0));
cout << opt << endl;
exit(0);
}
int mx = INT_MIN;
for (int i=0; i<n; i++) mx = max(mx, solve(0, i));
cout << mx << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
98296 KB |
Output is correct |
2 |
Correct |
58 ms |
98296 KB |
Output is correct |
3 |
Correct |
52 ms |
98296 KB |
Output is correct |
4 |
Correct |
58 ms |
98296 KB |
Output is correct |
5 |
Correct |
57 ms |
98424 KB |
Output is correct |
6 |
Correct |
58 ms |
98296 KB |
Output is correct |
7 |
Correct |
58 ms |
98424 KB |
Output is correct |
8 |
Correct |
53 ms |
98296 KB |
Output is correct |
9 |
Correct |
54 ms |
98424 KB |
Output is correct |
10 |
Correct |
57 ms |
98296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
98424 KB |
Output is correct |
2 |
Correct |
53 ms |
98424 KB |
Output is correct |
3 |
Correct |
59 ms |
98424 KB |
Output is correct |
4 |
Correct |
57 ms |
98432 KB |
Output is correct |
5 |
Correct |
54 ms |
98424 KB |
Output is correct |
6 |
Correct |
57 ms |
98424 KB |
Output is correct |
7 |
Correct |
53 ms |
98424 KB |
Output is correct |
8 |
Correct |
52 ms |
98432 KB |
Output is correct |
9 |
Correct |
57 ms |
98296 KB |
Output is correct |
10 |
Correct |
59 ms |
98424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
98680 KB |
Output is correct |
2 |
Correct |
66 ms |
98748 KB |
Output is correct |
3 |
Correct |
172 ms |
98888 KB |
Output is correct |
4 |
Correct |
359 ms |
99064 KB |
Output is correct |
5 |
Correct |
176 ms |
98808 KB |
Output is correct |
6 |
Correct |
197 ms |
98808 KB |
Output is correct |
7 |
Correct |
288 ms |
99064 KB |
Output is correct |
8 |
Correct |
127 ms |
98996 KB |
Output is correct |
9 |
Correct |
228 ms |
98936 KB |
Output is correct |
10 |
Correct |
451 ms |
99224 KB |
Output is correct |
11 |
Correct |
436 ms |
99324 KB |
Output is correct |
12 |
Correct |
444 ms |
99192 KB |
Output is correct |
13 |
Correct |
431 ms |
99192 KB |
Output is correct |
14 |
Correct |
59 ms |
98424 KB |
Output is correct |
15 |
Correct |
59 ms |
98424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
98680 KB |
Output is correct |
2 |
Correct |
66 ms |
98748 KB |
Output is correct |
3 |
Correct |
172 ms |
98888 KB |
Output is correct |
4 |
Correct |
359 ms |
99064 KB |
Output is correct |
5 |
Correct |
176 ms |
98808 KB |
Output is correct |
6 |
Correct |
197 ms |
98808 KB |
Output is correct |
7 |
Correct |
288 ms |
99064 KB |
Output is correct |
8 |
Correct |
127 ms |
98996 KB |
Output is correct |
9 |
Correct |
228 ms |
98936 KB |
Output is correct |
10 |
Correct |
451 ms |
99224 KB |
Output is correct |
11 |
Correct |
436 ms |
99324 KB |
Output is correct |
12 |
Correct |
444 ms |
99192 KB |
Output is correct |
13 |
Correct |
431 ms |
99192 KB |
Output is correct |
14 |
Correct |
59 ms |
98424 KB |
Output is correct |
15 |
Correct |
59 ms |
98424 KB |
Output is correct |
16 |
Correct |
249 ms |
99064 KB |
Output is correct |
17 |
Correct |
287 ms |
99064 KB |
Output is correct |
18 |
Correct |
258 ms |
98936 KB |
Output is correct |
19 |
Correct |
107 ms |
98680 KB |
Output is correct |
20 |
Correct |
207 ms |
98936 KB |
Output is correct |
21 |
Correct |
67 ms |
98552 KB |
Output is correct |
22 |
Correct |
131 ms |
98680 KB |
Output is correct |
23 |
Correct |
163 ms |
98836 KB |
Output is correct |
24 |
Correct |
85 ms |
98680 KB |
Output is correct |
25 |
Correct |
447 ms |
99324 KB |
Output is correct |
26 |
Correct |
445 ms |
99320 KB |
Output is correct |
27 |
Correct |
451 ms |
99320 KB |
Output is correct |
28 |
Correct |
438 ms |
99320 KB |
Output is correct |
29 |
Correct |
55 ms |
98424 KB |
Output is correct |
30 |
Correct |
55 ms |
98424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
98680 KB |
Output is correct |
2 |
Correct |
66 ms |
98748 KB |
Output is correct |
3 |
Correct |
172 ms |
98888 KB |
Output is correct |
4 |
Correct |
359 ms |
99064 KB |
Output is correct |
5 |
Correct |
176 ms |
98808 KB |
Output is correct |
6 |
Correct |
197 ms |
98808 KB |
Output is correct |
7 |
Correct |
288 ms |
99064 KB |
Output is correct |
8 |
Correct |
127 ms |
98996 KB |
Output is correct |
9 |
Correct |
228 ms |
98936 KB |
Output is correct |
10 |
Correct |
451 ms |
99224 KB |
Output is correct |
11 |
Correct |
436 ms |
99324 KB |
Output is correct |
12 |
Correct |
444 ms |
99192 KB |
Output is correct |
13 |
Correct |
431 ms |
99192 KB |
Output is correct |
14 |
Correct |
59 ms |
98424 KB |
Output is correct |
15 |
Correct |
59 ms |
98424 KB |
Output is correct |
16 |
Execution timed out |
2090 ms |
98684 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
98504 KB |
Output is correct |
2 |
Correct |
59 ms |
98296 KB |
Output is correct |
3 |
Correct |
57 ms |
98296 KB |
Output is correct |
4 |
Correct |
54 ms |
98296 KB |
Output is correct |
5 |
Correct |
56 ms |
98304 KB |
Output is correct |
6 |
Correct |
54 ms |
98296 KB |
Output is correct |
7 |
Correct |
54 ms |
98316 KB |
Output is correct |
8 |
Correct |
59 ms |
98296 KB |
Output is correct |
9 |
Correct |
62 ms |
98424 KB |
Output is correct |
10 |
Correct |
57 ms |
98424 KB |
Output is correct |
11 |
Correct |
56 ms |
98424 KB |
Output is correct |
12 |
Correct |
65 ms |
98296 KB |
Output is correct |
13 |
Correct |
59 ms |
98296 KB |
Output is correct |
14 |
Correct |
58 ms |
98296 KB |
Output is correct |
15 |
Correct |
53 ms |
98296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
98296 KB |
Output is correct |
2 |
Correct |
58 ms |
98296 KB |
Output is correct |
3 |
Correct |
52 ms |
98296 KB |
Output is correct |
4 |
Correct |
58 ms |
98296 KB |
Output is correct |
5 |
Correct |
57 ms |
98424 KB |
Output is correct |
6 |
Correct |
58 ms |
98296 KB |
Output is correct |
7 |
Correct |
58 ms |
98424 KB |
Output is correct |
8 |
Correct |
53 ms |
98296 KB |
Output is correct |
9 |
Correct |
54 ms |
98424 KB |
Output is correct |
10 |
Correct |
57 ms |
98296 KB |
Output is correct |
11 |
Correct |
59 ms |
98424 KB |
Output is correct |
12 |
Correct |
53 ms |
98424 KB |
Output is correct |
13 |
Correct |
59 ms |
98424 KB |
Output is correct |
14 |
Correct |
57 ms |
98432 KB |
Output is correct |
15 |
Correct |
54 ms |
98424 KB |
Output is correct |
16 |
Correct |
57 ms |
98424 KB |
Output is correct |
17 |
Correct |
53 ms |
98424 KB |
Output is correct |
18 |
Correct |
52 ms |
98432 KB |
Output is correct |
19 |
Correct |
57 ms |
98296 KB |
Output is correct |
20 |
Correct |
59 ms |
98424 KB |
Output is correct |
21 |
Correct |
92 ms |
98680 KB |
Output is correct |
22 |
Correct |
66 ms |
98748 KB |
Output is correct |
23 |
Correct |
172 ms |
98888 KB |
Output is correct |
24 |
Correct |
359 ms |
99064 KB |
Output is correct |
25 |
Correct |
176 ms |
98808 KB |
Output is correct |
26 |
Correct |
197 ms |
98808 KB |
Output is correct |
27 |
Correct |
288 ms |
99064 KB |
Output is correct |
28 |
Correct |
127 ms |
98996 KB |
Output is correct |
29 |
Correct |
228 ms |
98936 KB |
Output is correct |
30 |
Correct |
451 ms |
99224 KB |
Output is correct |
31 |
Correct |
436 ms |
99324 KB |
Output is correct |
32 |
Correct |
444 ms |
99192 KB |
Output is correct |
33 |
Correct |
431 ms |
99192 KB |
Output is correct |
34 |
Correct |
59 ms |
98424 KB |
Output is correct |
35 |
Correct |
59 ms |
98424 KB |
Output is correct |
36 |
Correct |
249 ms |
99064 KB |
Output is correct |
37 |
Correct |
287 ms |
99064 KB |
Output is correct |
38 |
Correct |
258 ms |
98936 KB |
Output is correct |
39 |
Correct |
107 ms |
98680 KB |
Output is correct |
40 |
Correct |
207 ms |
98936 KB |
Output is correct |
41 |
Correct |
67 ms |
98552 KB |
Output is correct |
42 |
Correct |
131 ms |
98680 KB |
Output is correct |
43 |
Correct |
163 ms |
98836 KB |
Output is correct |
44 |
Correct |
85 ms |
98680 KB |
Output is correct |
45 |
Correct |
447 ms |
99324 KB |
Output is correct |
46 |
Correct |
445 ms |
99320 KB |
Output is correct |
47 |
Correct |
451 ms |
99320 KB |
Output is correct |
48 |
Correct |
438 ms |
99320 KB |
Output is correct |
49 |
Correct |
55 ms |
98424 KB |
Output is correct |
50 |
Correct |
55 ms |
98424 KB |
Output is correct |
51 |
Execution timed out |
2090 ms |
98684 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |