#include <iostream>
using namespace std;
int v[1005];
int s[5005];
int t[5005];
int dp[2][5005][2][2];
void maxi (int &a, int b)
{
a = max(a, b);
}
int main()
{
int k, n, m, a, b, j, i, ans = 0;
cin >> k >> n >> m >> a >> b;
ans = a + m * 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 (int st1 = 0; st1 < 2; st1++)
for (int st2 = 0; st2 < 2; st2++)
dp[0][j][st1][st2] = -1e9;
for (i = 1; i <= n; i++)
{
for (int st1 = 0; st1 < 2; st1++)
for (int st2 = 0; st2 < 2; st2++)
dp[i % 2][0][st1][st2] = -1e9;
for (j = 1; j <= m; j++)
for (int st1 = 0; st1 < 2; st1++)
for (int st2 = 0; st2 < 2; st2++)
{
if (st1 && st2)
{
if (s[i] == t[j])
{
int var = - 2e9;
maxi(var, dp[1 - i % 2][j - 1][0][0] + v[s[i]]);
maxi(var, dp[1 - i % 2][j - 1][0][1] + v[s[i]]);
maxi(var, dp[1 - i % 2][j - 1][1][0] + v[s[i]]);
maxi(var, dp[1 - i % 2][j - 1][1][1] + v[s[i]]);
if (j == 1)
maxi(var, v[s[i]]);
else
maxi(var, a + b * (j - 1) + v[s[i]]);
dp[i % 2][j][st1][st2] = var;
}
else
dp[i % 2][j][st1][st2] = -1e9;
}
else
if (st2)
dp[i % 2][j][st1][st2] = max(dp[1 - i % 2][j][0][1] + b, dp[1 - i % 2][j][1][1] + a + b);
else
if (st1)
dp[i % 2][j][st1][st2] = max(dp[i % 2][j - 1][1][0] + b, dp[i % 2][j - 1][1][1] + a + b);
else
dp[i % 2][j][st1][st2] = max(dp[i % 2][j - 1][0][0] + b, dp[i % 2][j - 1][0][1] + a + b);
if (j == m)
maxi(ans, dp[i % 2][j][st1][st2]);
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
12 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
7 ms |
364 KB |
Output is correct |
10 |
Correct |
15 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
7 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
12 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
14 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
364 KB |
Output is correct |
2 |
Correct |
11 ms |
364 KB |
Output is correct |
3 |
Correct |
90 ms |
364 KB |
Output is correct |
4 |
Correct |
217 ms |
620 KB |
Output is correct |
5 |
Correct |
67 ms |
512 KB |
Output is correct |
6 |
Correct |
95 ms |
548 KB |
Output is correct |
7 |
Correct |
173 ms |
560 KB |
Output is correct |
8 |
Correct |
60 ms |
364 KB |
Output is correct |
9 |
Correct |
112 ms |
492 KB |
Output is correct |
10 |
Correct |
261 ms |
620 KB |
Output is correct |
11 |
Correct |
267 ms |
492 KB |
Output is correct |
12 |
Correct |
263 ms |
620 KB |
Output is correct |
13 |
Correct |
263 ms |
620 KB |
Output is correct |
14 |
Correct |
323 ms |
492 KB |
Output is correct |
15 |
Correct |
317 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
364 KB |
Output is correct |
2 |
Correct |
11 ms |
364 KB |
Output is correct |
3 |
Correct |
90 ms |
364 KB |
Output is correct |
4 |
Correct |
217 ms |
620 KB |
Output is correct |
5 |
Correct |
67 ms |
512 KB |
Output is correct |
6 |
Correct |
95 ms |
548 KB |
Output is correct |
7 |
Correct |
173 ms |
560 KB |
Output is correct |
8 |
Correct |
60 ms |
364 KB |
Output is correct |
9 |
Correct |
112 ms |
492 KB |
Output is correct |
10 |
Correct |
261 ms |
620 KB |
Output is correct |
11 |
Correct |
267 ms |
492 KB |
Output is correct |
12 |
Correct |
263 ms |
620 KB |
Output is correct |
13 |
Correct |
263 ms |
620 KB |
Output is correct |
14 |
Correct |
323 ms |
492 KB |
Output is correct |
15 |
Correct |
317 ms |
620 KB |
Output is correct |
16 |
Correct |
140 ms |
492 KB |
Output is correct |
17 |
Correct |
176 ms |
620 KB |
Output is correct |
18 |
Correct |
153 ms |
508 KB |
Output is correct |
19 |
Correct |
32 ms |
492 KB |
Output is correct |
20 |
Correct |
108 ms |
492 KB |
Output is correct |
21 |
Correct |
7 ms |
364 KB |
Output is correct |
22 |
Correct |
38 ms |
492 KB |
Output is correct |
23 |
Correct |
71 ms |
492 KB |
Output is correct |
24 |
Correct |
24 ms |
364 KB |
Output is correct |
25 |
Correct |
276 ms |
620 KB |
Output is correct |
26 |
Correct |
272 ms |
612 KB |
Output is correct |
27 |
Correct |
275 ms |
620 KB |
Output is correct |
28 |
Correct |
269 ms |
620 KB |
Output is correct |
29 |
Correct |
324 ms |
620 KB |
Output is correct |
30 |
Correct |
327 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
364 KB |
Output is correct |
2 |
Correct |
11 ms |
364 KB |
Output is correct |
3 |
Correct |
90 ms |
364 KB |
Output is correct |
4 |
Correct |
217 ms |
620 KB |
Output is correct |
5 |
Correct |
67 ms |
512 KB |
Output is correct |
6 |
Correct |
95 ms |
548 KB |
Output is correct |
7 |
Correct |
173 ms |
560 KB |
Output is correct |
8 |
Correct |
60 ms |
364 KB |
Output is correct |
9 |
Correct |
112 ms |
492 KB |
Output is correct |
10 |
Correct |
261 ms |
620 KB |
Output is correct |
11 |
Correct |
267 ms |
492 KB |
Output is correct |
12 |
Correct |
263 ms |
620 KB |
Output is correct |
13 |
Correct |
263 ms |
620 KB |
Output is correct |
14 |
Correct |
323 ms |
492 KB |
Output is correct |
15 |
Correct |
317 ms |
620 KB |
Output is correct |
16 |
Correct |
228 ms |
508 KB |
Output is correct |
17 |
Correct |
108 ms |
492 KB |
Output is correct |
18 |
Correct |
32 ms |
492 KB |
Output is correct |
19 |
Correct |
9 ms |
364 KB |
Output is correct |
20 |
Correct |
6 ms |
364 KB |
Output is correct |
21 |
Correct |
8 ms |
492 KB |
Output is correct |
22 |
Correct |
120 ms |
620 KB |
Output is correct |
23 |
Correct |
19 ms |
364 KB |
Output is correct |
24 |
Correct |
190 ms |
492 KB |
Output is correct |
25 |
Correct |
278 ms |
620 KB |
Output is correct |
26 |
Correct |
281 ms |
492 KB |
Output is correct |
27 |
Correct |
273 ms |
492 KB |
Output is correct |
28 |
Correct |
266 ms |
492 KB |
Output is correct |
29 |
Correct |
312 ms |
492 KB |
Output is correct |
30 |
Correct |
320 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
12 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
7 ms |
364 KB |
Output is correct |
10 |
Correct |
15 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
4 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
7 ms |
364 KB |
Output is correct |
17 |
Correct |
7 ms |
364 KB |
Output is correct |
18 |
Correct |
12 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
14 ms |
364 KB |
Output is correct |
21 |
Correct |
27 ms |
364 KB |
Output is correct |
22 |
Correct |
11 ms |
364 KB |
Output is correct |
23 |
Correct |
90 ms |
364 KB |
Output is correct |
24 |
Correct |
217 ms |
620 KB |
Output is correct |
25 |
Correct |
67 ms |
512 KB |
Output is correct |
26 |
Correct |
95 ms |
548 KB |
Output is correct |
27 |
Correct |
173 ms |
560 KB |
Output is correct |
28 |
Correct |
60 ms |
364 KB |
Output is correct |
29 |
Correct |
112 ms |
492 KB |
Output is correct |
30 |
Correct |
261 ms |
620 KB |
Output is correct |
31 |
Correct |
267 ms |
492 KB |
Output is correct |
32 |
Correct |
263 ms |
620 KB |
Output is correct |
33 |
Correct |
263 ms |
620 KB |
Output is correct |
34 |
Correct |
323 ms |
492 KB |
Output is correct |
35 |
Correct |
317 ms |
620 KB |
Output is correct |
36 |
Correct |
140 ms |
492 KB |
Output is correct |
37 |
Correct |
176 ms |
620 KB |
Output is correct |
38 |
Correct |
153 ms |
508 KB |
Output is correct |
39 |
Correct |
32 ms |
492 KB |
Output is correct |
40 |
Correct |
108 ms |
492 KB |
Output is correct |
41 |
Correct |
7 ms |
364 KB |
Output is correct |
42 |
Correct |
38 ms |
492 KB |
Output is correct |
43 |
Correct |
71 ms |
492 KB |
Output is correct |
44 |
Correct |
24 ms |
364 KB |
Output is correct |
45 |
Correct |
276 ms |
620 KB |
Output is correct |
46 |
Correct |
272 ms |
612 KB |
Output is correct |
47 |
Correct |
275 ms |
620 KB |
Output is correct |
48 |
Correct |
269 ms |
620 KB |
Output is correct |
49 |
Correct |
324 ms |
620 KB |
Output is correct |
50 |
Correct |
327 ms |
620 KB |
Output is correct |
51 |
Correct |
228 ms |
508 KB |
Output is correct |
52 |
Correct |
108 ms |
492 KB |
Output is correct |
53 |
Correct |
32 ms |
492 KB |
Output is correct |
54 |
Correct |
9 ms |
364 KB |
Output is correct |
55 |
Correct |
6 ms |
364 KB |
Output is correct |
56 |
Correct |
8 ms |
492 KB |
Output is correct |
57 |
Correct |
120 ms |
620 KB |
Output is correct |
58 |
Correct |
19 ms |
364 KB |
Output is correct |
59 |
Correct |
190 ms |
492 KB |
Output is correct |
60 |
Correct |
278 ms |
620 KB |
Output is correct |
61 |
Correct |
281 ms |
492 KB |
Output is correct |
62 |
Correct |
273 ms |
492 KB |
Output is correct |
63 |
Correct |
266 ms |
492 KB |
Output is correct |
64 |
Correct |
312 ms |
492 KB |
Output is correct |
65 |
Correct |
320 ms |
592 KB |
Output is correct |
66 |
Correct |
1 ms |
364 KB |
Output is correct |
67 |
Correct |
0 ms |
364 KB |
Output is correct |
68 |
Correct |
1 ms |
364 KB |
Output is correct |
69 |
Correct |
1 ms |
364 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Correct |
0 ms |
364 KB |
Output is correct |
73 |
Correct |
1 ms |
364 KB |
Output is correct |
74 |
Correct |
1 ms |
364 KB |
Output is correct |
75 |
Correct |
1 ms |
364 KB |
Output is correct |
76 |
Correct |
1 ms |
364 KB |
Output is correct |
77 |
Correct |
1 ms |
364 KB |
Output is correct |
78 |
Correct |
1 ms |
364 KB |
Output is correct |
79 |
Correct |
1 ms |
364 KB |
Output is correct |
80 |
Correct |
1 ms |
364 KB |
Output is correct |
81 |
Correct |
219 ms |
492 KB |
Output is correct |
82 |
Correct |
51 ms |
364 KB |
Output is correct |
83 |
Correct |
96 ms |
492 KB |
Output is correct |
84 |
Correct |
52 ms |
620 KB |
Output is correct |
85 |
Correct |
87 ms |
492 KB |
Output is correct |
86 |
Correct |
75 ms |
492 KB |
Output is correct |
87 |
Correct |
13 ms |
364 KB |
Output is correct |
88 |
Correct |
8 ms |
364 KB |
Output is correct |
89 |
Correct |
32 ms |
364 KB |
Output is correct |
90 |
Correct |
304 ms |
592 KB |
Output is correct |
91 |
Correct |
280 ms |
608 KB |
Output is correct |
92 |
Correct |
260 ms |
492 KB |
Output is correct |
93 |
Correct |
270 ms |
620 KB |
Output is correct |
94 |
Correct |
273 ms |
492 KB |
Output is correct |
95 |
Correct |
267 ms |
620 KB |
Output is correct |
96 |
Correct |
269 ms |
604 KB |
Output is correct |
97 |
Correct |
274 ms |
492 KB |
Output is correct |
98 |
Correct |
333 ms |
492 KB |
Output is correct |
99 |
Correct |
323 ms |
620 KB |
Output is correct |
100 |
Correct |
321 ms |
620 KB |
Output is correct |