#include <iostream>
#define int long long
using namespace std;
const int INF=1e18;
int dp[2][5001][2][2],k,n,m,a,b,v[1001],s[5001],t[5001],res=-INF;
int32_t main(){
ios_base::sync_with_stdio(NULL);
cin >> k >> n >> m >> a >> b;
for (int i=1;i<=k;i++)
cin >> v[i];
for (int i=1;i<=n;i++)
cin >> s[i];
for (int i=0;i<m;i++)
cin >> t[i];
for (int i=0;i<=m;i++){
for (int j=0;j<2;j++)
for (int k=0;k<2;k++){
dp[0][i][j][k]=(i==m?0:a*k+b*(m-i));
dp[1][i][j][k]=-INF;
}
}
for (int i=n;i>=1;i--){
for (int j=m;j>=0;j--){
for (int k=0;k<2;k++)
for (int l=0;l<2;l++){
dp[1][j][k][l]=max((j==m?0:a*k+b*(m-j)),dp[0][j][0][l]+a*k+b);
if (j==m)
continue;
dp[1][j][k][l]=max(dp[1][j][k][l],max((s[i]==t[j]?dp[0][j+1][1][1]+v[t[j]]:-INF),dp[1][j+1][k][0]+b+a*l));
}
}
res=max(res,dp[1][0][1][1]);
for (int j=m;j>=0;j--){
for (int k=0;k<2;k++)
for (int l=0;l<2;l++){
swap(dp[0][j][k][l],dp[1][j][k][l]);
dp[1][j][k][l]=-INF;
}
}
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
20 ms |
412 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
372 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
340 KB |
Output is correct |
10 |
Correct |
18 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
396 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
12 ms |
340 KB |
Output is correct |
7 |
Correct |
9 ms |
340 KB |
Output is correct |
8 |
Correct |
12 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
18 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
340 KB |
Output is correct |
2 |
Correct |
17 ms |
388 KB |
Output is correct |
3 |
Correct |
133 ms |
508 KB |
Output is correct |
4 |
Correct |
365 ms |
724 KB |
Output is correct |
5 |
Correct |
88 ms |
596 KB |
Output is correct |
6 |
Correct |
128 ms |
624 KB |
Output is correct |
7 |
Correct |
265 ms |
636 KB |
Output is correct |
8 |
Correct |
79 ms |
480 KB |
Output is correct |
9 |
Correct |
152 ms |
628 KB |
Output is correct |
10 |
Correct |
378 ms |
744 KB |
Output is correct |
11 |
Correct |
390 ms |
744 KB |
Output is correct |
12 |
Correct |
382 ms |
740 KB |
Output is correct |
13 |
Correct |
407 ms |
724 KB |
Output is correct |
14 |
Correct |
447 ms |
724 KB |
Output is correct |
15 |
Correct |
434 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
340 KB |
Output is correct |
2 |
Correct |
17 ms |
388 KB |
Output is correct |
3 |
Correct |
133 ms |
508 KB |
Output is correct |
4 |
Correct |
365 ms |
724 KB |
Output is correct |
5 |
Correct |
88 ms |
596 KB |
Output is correct |
6 |
Correct |
128 ms |
624 KB |
Output is correct |
7 |
Correct |
265 ms |
636 KB |
Output is correct |
8 |
Correct |
79 ms |
480 KB |
Output is correct |
9 |
Correct |
152 ms |
628 KB |
Output is correct |
10 |
Correct |
378 ms |
744 KB |
Output is correct |
11 |
Correct |
390 ms |
744 KB |
Output is correct |
12 |
Correct |
382 ms |
740 KB |
Output is correct |
13 |
Correct |
407 ms |
724 KB |
Output is correct |
14 |
Correct |
447 ms |
724 KB |
Output is correct |
15 |
Correct |
434 ms |
724 KB |
Output is correct |
16 |
Correct |
212 ms |
628 KB |
Output is correct |
17 |
Correct |
243 ms |
624 KB |
Output is correct |
18 |
Correct |
229 ms |
596 KB |
Output is correct |
19 |
Correct |
43 ms |
540 KB |
Output is correct |
20 |
Correct |
171 ms |
568 KB |
Output is correct |
21 |
Correct |
9 ms |
340 KB |
Output is correct |
22 |
Correct |
51 ms |
596 KB |
Output is correct |
23 |
Correct |
99 ms |
592 KB |
Output is correct |
24 |
Correct |
36 ms |
368 KB |
Output is correct |
25 |
Correct |
395 ms |
740 KB |
Output is correct |
26 |
Correct |
405 ms |
748 KB |
Output is correct |
27 |
Correct |
389 ms |
736 KB |
Output is correct |
28 |
Correct |
390 ms |
732 KB |
Output is correct |
29 |
Correct |
443 ms |
848 KB |
Output is correct |
30 |
Correct |
461 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
340 KB |
Output is correct |
2 |
Correct |
17 ms |
388 KB |
Output is correct |
3 |
Correct |
133 ms |
508 KB |
Output is correct |
4 |
Correct |
365 ms |
724 KB |
Output is correct |
5 |
Correct |
88 ms |
596 KB |
Output is correct |
6 |
Correct |
128 ms |
624 KB |
Output is correct |
7 |
Correct |
265 ms |
636 KB |
Output is correct |
8 |
Correct |
79 ms |
480 KB |
Output is correct |
9 |
Correct |
152 ms |
628 KB |
Output is correct |
10 |
Correct |
378 ms |
744 KB |
Output is correct |
11 |
Correct |
390 ms |
744 KB |
Output is correct |
12 |
Correct |
382 ms |
740 KB |
Output is correct |
13 |
Correct |
407 ms |
724 KB |
Output is correct |
14 |
Correct |
447 ms |
724 KB |
Output is correct |
15 |
Correct |
434 ms |
724 KB |
Output is correct |
16 |
Correct |
332 ms |
688 KB |
Output is correct |
17 |
Correct |
172 ms |
532 KB |
Output is correct |
18 |
Correct |
47 ms |
468 KB |
Output is correct |
19 |
Correct |
12 ms |
340 KB |
Output is correct |
20 |
Correct |
8 ms |
468 KB |
Output is correct |
21 |
Correct |
9 ms |
596 KB |
Output is correct |
22 |
Correct |
144 ms |
596 KB |
Output is correct |
23 |
Correct |
27 ms |
432 KB |
Output is correct |
24 |
Correct |
269 ms |
636 KB |
Output is correct |
25 |
Correct |
426 ms |
744 KB |
Output is correct |
26 |
Correct |
407 ms |
724 KB |
Output is correct |
27 |
Correct |
427 ms |
744 KB |
Output is correct |
28 |
Correct |
384 ms |
844 KB |
Output is correct |
29 |
Correct |
420 ms |
724 KB |
Output is correct |
30 |
Correct |
504 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
20 ms |
412 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
372 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
340 KB |
Output is correct |
10 |
Correct |
18 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
12 ms |
340 KB |
Output is correct |
17 |
Correct |
9 ms |
340 KB |
Output is correct |
18 |
Correct |
12 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
18 ms |
432 KB |
Output is correct |
21 |
Correct |
37 ms |
340 KB |
Output is correct |
22 |
Correct |
17 ms |
388 KB |
Output is correct |
23 |
Correct |
133 ms |
508 KB |
Output is correct |
24 |
Correct |
365 ms |
724 KB |
Output is correct |
25 |
Correct |
88 ms |
596 KB |
Output is correct |
26 |
Correct |
128 ms |
624 KB |
Output is correct |
27 |
Correct |
265 ms |
636 KB |
Output is correct |
28 |
Correct |
79 ms |
480 KB |
Output is correct |
29 |
Correct |
152 ms |
628 KB |
Output is correct |
30 |
Correct |
378 ms |
744 KB |
Output is correct |
31 |
Correct |
390 ms |
744 KB |
Output is correct |
32 |
Correct |
382 ms |
740 KB |
Output is correct |
33 |
Correct |
407 ms |
724 KB |
Output is correct |
34 |
Correct |
447 ms |
724 KB |
Output is correct |
35 |
Correct |
434 ms |
724 KB |
Output is correct |
36 |
Correct |
212 ms |
628 KB |
Output is correct |
37 |
Correct |
243 ms |
624 KB |
Output is correct |
38 |
Correct |
229 ms |
596 KB |
Output is correct |
39 |
Correct |
43 ms |
540 KB |
Output is correct |
40 |
Correct |
171 ms |
568 KB |
Output is correct |
41 |
Correct |
9 ms |
340 KB |
Output is correct |
42 |
Correct |
51 ms |
596 KB |
Output is correct |
43 |
Correct |
99 ms |
592 KB |
Output is correct |
44 |
Correct |
36 ms |
368 KB |
Output is correct |
45 |
Correct |
395 ms |
740 KB |
Output is correct |
46 |
Correct |
405 ms |
748 KB |
Output is correct |
47 |
Correct |
389 ms |
736 KB |
Output is correct |
48 |
Correct |
390 ms |
732 KB |
Output is correct |
49 |
Correct |
443 ms |
848 KB |
Output is correct |
50 |
Correct |
461 ms |
724 KB |
Output is correct |
51 |
Correct |
332 ms |
688 KB |
Output is correct |
52 |
Correct |
172 ms |
532 KB |
Output is correct |
53 |
Correct |
47 ms |
468 KB |
Output is correct |
54 |
Correct |
12 ms |
340 KB |
Output is correct |
55 |
Correct |
8 ms |
468 KB |
Output is correct |
56 |
Correct |
9 ms |
596 KB |
Output is correct |
57 |
Correct |
144 ms |
596 KB |
Output is correct |
58 |
Correct |
27 ms |
432 KB |
Output is correct |
59 |
Correct |
269 ms |
636 KB |
Output is correct |
60 |
Correct |
426 ms |
744 KB |
Output is correct |
61 |
Correct |
407 ms |
724 KB |
Output is correct |
62 |
Correct |
427 ms |
744 KB |
Output is correct |
63 |
Correct |
384 ms |
844 KB |
Output is correct |
64 |
Correct |
420 ms |
724 KB |
Output is correct |
65 |
Correct |
504 ms |
724 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
0 ms |
340 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
0 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
0 ms |
340 KB |
Output is correct |
76 |
Correct |
1 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
352 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |
81 |
Correct |
314 ms |
696 KB |
Output is correct |
82 |
Correct |
75 ms |
468 KB |
Output is correct |
83 |
Correct |
135 ms |
596 KB |
Output is correct |
84 |
Correct |
80 ms |
596 KB |
Output is correct |
85 |
Correct |
134 ms |
564 KB |
Output is correct |
86 |
Correct |
102 ms |
516 KB |
Output is correct |
87 |
Correct |
19 ms |
340 KB |
Output is correct |
88 |
Correct |
12 ms |
340 KB |
Output is correct |
89 |
Correct |
48 ms |
468 KB |
Output is correct |
90 |
Correct |
416 ms |
724 KB |
Output is correct |
91 |
Correct |
385 ms |
740 KB |
Output is correct |
92 |
Correct |
406 ms |
736 KB |
Output is correct |
93 |
Correct |
374 ms |
740 KB |
Output is correct |
94 |
Correct |
377 ms |
740 KB |
Output is correct |
95 |
Correct |
401 ms |
720 KB |
Output is correct |
96 |
Correct |
409 ms |
736 KB |
Output is correct |
97 |
Correct |
383 ms |
732 KB |
Output is correct |
98 |
Correct |
446 ms |
724 KB |
Output is correct |
99 |
Correct |
425 ms |
724 KB |
Output is correct |
100 |
Correct |
425 ms |
720 KB |
Output is correct |