#include <bits/stdc++.h>
using namespace std;
const int Nmax = 5005, inf = 1e9;
int dp[2][Nmax][2][2];
int V[Nmax], S[Nmax], T[Nmax], A, B, n, m, k;
int best(int i, int j)
{
int r = dp[i][j][0][0];
r = max(r, dp[i][j][1][0]);
r = max(r, dp[i][j][0][1]);
r = max(r, dp[i][j][1][1]);
if(j) r = max(r, -A-B*j);
else r = max(r, 0);
return r;
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> k >> n >> m >> A >> B;
A = -A, B = -B;
int i, j, a, b;
for(i=1; i<=k; ++i) cin >> V[i];
for(i=1; i<=n; ++i) cin >> S[i];
for(i=1; i<=m; ++i) cin >> T[i];
for(j=1; j<=m; ++j)
{
for(a=0; a<2; ++a)
for(b=0; b<2; ++b)
dp[0][j][a][b] = -inf;
}
int ans = -A-m*B, r;
for(i=1, r=1; r<=n; ++r, i^=1)
{
for(a=0; a<2; ++a)
for(b=0; b<2; ++b)
dp[i][0][a][b] = -inf;
for(j=1; j<=m; ++j)
for(a=0; a<2; ++a)
for(b=0; b<2; ++b)
{
if(a == 1 && b == 1)
{
if(S[r] == T[j])
dp[i][j][a][b] = best(i^1, j-1) + V[S[r]];
else dp[i][j][a][b] = -inf;
continue;
}
if(a == 1 && b == 0)
{
dp[i][j][a][b] = max(dp[i][j-1][1][0] - B, dp[i][j-1][1][1] - A - B);
continue;
}
if(a == 0 && b == 1)
{
dp[i][j][a][b] = max(dp[i^1][j][0][1] - B, dp[i^1][j][1][1] - A - B);
continue;
}
if(a == 0 && b == 0)
{
dp[i][j][a][b] = max(dp[i][j-1][0][0] - B, dp[i][j-1][0][1] - A - B);
dp[i][j][a][b] = max(dp[i][j][a][b], -2*A-B*(j+1));
continue;
}
}
ans = max(ans, best(i, m));
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
248 KB |
Output is correct |
2 |
Correct |
20 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
9 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
376 KB |
Output is correct |
9 |
Correct |
13 ms |
376 KB |
Output is correct |
10 |
Correct |
23 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
8 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
13 ms |
376 KB |
Output is correct |
7 |
Correct |
13 ms |
376 KB |
Output is correct |
8 |
Correct |
18 ms |
376 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
21 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
376 KB |
Output is correct |
2 |
Correct |
18 ms |
376 KB |
Output is correct |
3 |
Correct |
116 ms |
504 KB |
Output is correct |
4 |
Correct |
276 ms |
504 KB |
Output is correct |
5 |
Correct |
93 ms |
504 KB |
Output is correct |
6 |
Correct |
125 ms |
504 KB |
Output is correct |
7 |
Correct |
220 ms |
504 KB |
Output is correct |
8 |
Correct |
76 ms |
376 KB |
Output is correct |
9 |
Correct |
147 ms |
508 KB |
Output is correct |
10 |
Correct |
342 ms |
580 KB |
Output is correct |
11 |
Correct |
340 ms |
508 KB |
Output is correct |
12 |
Correct |
341 ms |
504 KB |
Output is correct |
13 |
Correct |
347 ms |
504 KB |
Output is correct |
14 |
Correct |
431 ms |
504 KB |
Output is correct |
15 |
Correct |
438 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
376 KB |
Output is correct |
2 |
Correct |
18 ms |
376 KB |
Output is correct |
3 |
Correct |
116 ms |
504 KB |
Output is correct |
4 |
Correct |
276 ms |
504 KB |
Output is correct |
5 |
Correct |
93 ms |
504 KB |
Output is correct |
6 |
Correct |
125 ms |
504 KB |
Output is correct |
7 |
Correct |
220 ms |
504 KB |
Output is correct |
8 |
Correct |
76 ms |
376 KB |
Output is correct |
9 |
Correct |
147 ms |
508 KB |
Output is correct |
10 |
Correct |
342 ms |
580 KB |
Output is correct |
11 |
Correct |
340 ms |
508 KB |
Output is correct |
12 |
Correct |
341 ms |
504 KB |
Output is correct |
13 |
Correct |
347 ms |
504 KB |
Output is correct |
14 |
Correct |
431 ms |
504 KB |
Output is correct |
15 |
Correct |
438 ms |
508 KB |
Output is correct |
16 |
Correct |
183 ms |
504 KB |
Output is correct |
17 |
Correct |
221 ms |
540 KB |
Output is correct |
18 |
Correct |
197 ms |
504 KB |
Output is correct |
19 |
Correct |
43 ms |
376 KB |
Output is correct |
20 |
Correct |
140 ms |
380 KB |
Output is correct |
21 |
Correct |
12 ms |
376 KB |
Output is correct |
22 |
Correct |
48 ms |
508 KB |
Output is correct |
23 |
Correct |
91 ms |
504 KB |
Output is correct |
24 |
Correct |
37 ms |
376 KB |
Output is correct |
25 |
Correct |
348 ms |
504 KB |
Output is correct |
26 |
Correct |
343 ms |
504 KB |
Output is correct |
27 |
Correct |
360 ms |
504 KB |
Output is correct |
28 |
Correct |
338 ms |
504 KB |
Output is correct |
29 |
Correct |
439 ms |
508 KB |
Output is correct |
30 |
Correct |
430 ms |
580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
376 KB |
Output is correct |
2 |
Correct |
18 ms |
376 KB |
Output is correct |
3 |
Correct |
116 ms |
504 KB |
Output is correct |
4 |
Correct |
276 ms |
504 KB |
Output is correct |
5 |
Correct |
93 ms |
504 KB |
Output is correct |
6 |
Correct |
125 ms |
504 KB |
Output is correct |
7 |
Correct |
220 ms |
504 KB |
Output is correct |
8 |
Correct |
76 ms |
376 KB |
Output is correct |
9 |
Correct |
147 ms |
508 KB |
Output is correct |
10 |
Correct |
342 ms |
580 KB |
Output is correct |
11 |
Correct |
340 ms |
508 KB |
Output is correct |
12 |
Correct |
341 ms |
504 KB |
Output is correct |
13 |
Correct |
347 ms |
504 KB |
Output is correct |
14 |
Correct |
431 ms |
504 KB |
Output is correct |
15 |
Correct |
438 ms |
508 KB |
Output is correct |
16 |
Correct |
295 ms |
504 KB |
Output is correct |
17 |
Correct |
140 ms |
376 KB |
Output is correct |
18 |
Correct |
46 ms |
376 KB |
Output is correct |
19 |
Correct |
16 ms |
376 KB |
Output is correct |
20 |
Correct |
10 ms |
376 KB |
Output is correct |
21 |
Correct |
12 ms |
504 KB |
Output is correct |
22 |
Correct |
135 ms |
508 KB |
Output is correct |
23 |
Correct |
29 ms |
376 KB |
Output is correct |
24 |
Correct |
238 ms |
504 KB |
Output is correct |
25 |
Correct |
344 ms |
508 KB |
Output is correct |
26 |
Correct |
345 ms |
504 KB |
Output is correct |
27 |
Correct |
347 ms |
632 KB |
Output is correct |
28 |
Correct |
344 ms |
376 KB |
Output is correct |
29 |
Correct |
417 ms |
504 KB |
Output is correct |
30 |
Correct |
426 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
380 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
248 KB |
Output is correct |
2 |
Correct |
20 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
9 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
376 KB |
Output is correct |
9 |
Correct |
13 ms |
376 KB |
Output is correct |
10 |
Correct |
23 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
8 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
13 ms |
376 KB |
Output is correct |
17 |
Correct |
13 ms |
376 KB |
Output is correct |
18 |
Correct |
18 ms |
376 KB |
Output is correct |
19 |
Correct |
6 ms |
376 KB |
Output is correct |
20 |
Correct |
21 ms |
376 KB |
Output is correct |
21 |
Correct |
38 ms |
376 KB |
Output is correct |
22 |
Correct |
18 ms |
376 KB |
Output is correct |
23 |
Correct |
116 ms |
504 KB |
Output is correct |
24 |
Correct |
276 ms |
504 KB |
Output is correct |
25 |
Correct |
93 ms |
504 KB |
Output is correct |
26 |
Correct |
125 ms |
504 KB |
Output is correct |
27 |
Correct |
220 ms |
504 KB |
Output is correct |
28 |
Correct |
76 ms |
376 KB |
Output is correct |
29 |
Correct |
147 ms |
508 KB |
Output is correct |
30 |
Correct |
342 ms |
580 KB |
Output is correct |
31 |
Correct |
340 ms |
508 KB |
Output is correct |
32 |
Correct |
341 ms |
504 KB |
Output is correct |
33 |
Correct |
347 ms |
504 KB |
Output is correct |
34 |
Correct |
431 ms |
504 KB |
Output is correct |
35 |
Correct |
438 ms |
508 KB |
Output is correct |
36 |
Correct |
183 ms |
504 KB |
Output is correct |
37 |
Correct |
221 ms |
540 KB |
Output is correct |
38 |
Correct |
197 ms |
504 KB |
Output is correct |
39 |
Correct |
43 ms |
376 KB |
Output is correct |
40 |
Correct |
140 ms |
380 KB |
Output is correct |
41 |
Correct |
12 ms |
376 KB |
Output is correct |
42 |
Correct |
48 ms |
508 KB |
Output is correct |
43 |
Correct |
91 ms |
504 KB |
Output is correct |
44 |
Correct |
37 ms |
376 KB |
Output is correct |
45 |
Correct |
348 ms |
504 KB |
Output is correct |
46 |
Correct |
343 ms |
504 KB |
Output is correct |
47 |
Correct |
360 ms |
504 KB |
Output is correct |
48 |
Correct |
338 ms |
504 KB |
Output is correct |
49 |
Correct |
439 ms |
508 KB |
Output is correct |
50 |
Correct |
430 ms |
580 KB |
Output is correct |
51 |
Correct |
295 ms |
504 KB |
Output is correct |
52 |
Correct |
140 ms |
376 KB |
Output is correct |
53 |
Correct |
46 ms |
376 KB |
Output is correct |
54 |
Correct |
16 ms |
376 KB |
Output is correct |
55 |
Correct |
10 ms |
376 KB |
Output is correct |
56 |
Correct |
12 ms |
504 KB |
Output is correct |
57 |
Correct |
135 ms |
508 KB |
Output is correct |
58 |
Correct |
29 ms |
376 KB |
Output is correct |
59 |
Correct |
238 ms |
504 KB |
Output is correct |
60 |
Correct |
344 ms |
508 KB |
Output is correct |
61 |
Correct |
345 ms |
504 KB |
Output is correct |
62 |
Correct |
347 ms |
632 KB |
Output is correct |
63 |
Correct |
344 ms |
376 KB |
Output is correct |
64 |
Correct |
417 ms |
504 KB |
Output is correct |
65 |
Correct |
426 ms |
504 KB |
Output is correct |
66 |
Correct |
5 ms |
376 KB |
Output is correct |
67 |
Correct |
5 ms |
376 KB |
Output is correct |
68 |
Correct |
5 ms |
376 KB |
Output is correct |
69 |
Correct |
5 ms |
376 KB |
Output is correct |
70 |
Correct |
5 ms |
376 KB |
Output is correct |
71 |
Correct |
5 ms |
380 KB |
Output is correct |
72 |
Correct |
5 ms |
376 KB |
Output is correct |
73 |
Correct |
5 ms |
376 KB |
Output is correct |
74 |
Correct |
5 ms |
380 KB |
Output is correct |
75 |
Correct |
5 ms |
376 KB |
Output is correct |
76 |
Correct |
5 ms |
376 KB |
Output is correct |
77 |
Correct |
5 ms |
376 KB |
Output is correct |
78 |
Correct |
5 ms |
376 KB |
Output is correct |
79 |
Correct |
5 ms |
376 KB |
Output is correct |
80 |
Correct |
5 ms |
376 KB |
Output is correct |
81 |
Correct |
276 ms |
504 KB |
Output is correct |
82 |
Correct |
69 ms |
376 KB |
Output is correct |
83 |
Correct |
133 ms |
508 KB |
Output is correct |
84 |
Correct |
71 ms |
504 KB |
Output is correct |
85 |
Correct |
111 ms |
504 KB |
Output is correct |
86 |
Correct |
99 ms |
376 KB |
Output is correct |
87 |
Correct |
21 ms |
376 KB |
Output is correct |
88 |
Correct |
15 ms |
380 KB |
Output is correct |
89 |
Correct |
42 ms |
376 KB |
Output is correct |
90 |
Correct |
374 ms |
504 KB |
Output is correct |
91 |
Correct |
347 ms |
504 KB |
Output is correct |
92 |
Correct |
350 ms |
504 KB |
Output is correct |
93 |
Correct |
343 ms |
504 KB |
Output is correct |
94 |
Correct |
343 ms |
504 KB |
Output is correct |
95 |
Correct |
349 ms |
504 KB |
Output is correct |
96 |
Correct |
347 ms |
584 KB |
Output is correct |
97 |
Correct |
349 ms |
504 KB |
Output is correct |
98 |
Correct |
441 ms |
504 KB |
Output is correct |
99 |
Correct |
427 ms |
504 KB |
Output is correct |
100 |
Correct |
440 ms |
580 KB |
Output is correct |