#include <bits/stdc++.h>
using namespace std;
long long k,n,m,a,b,dp[5010][3][3],tab[5010][3][3],ans,arr[1010],s[5010],t[5010],dep[5005][5005];
int main(){
cin>>k>>n>>m>>a>>b;
for(long long i =1;i<=k;i++){
cin>>arr[i];
}
for(long long i =1;i<=n;i++){
cin>>s[i];
}
for(long long i =1;i<=m;i++){
cin>>t[i];
}
if(a==b&&a==0){
for(int i =1;i<=n;i++){
for(int j =1;j<=m;j++){
if(s[i]==t[j])dep[i][j]= arr[s[i]]+ dep[i-1][j-1];
dep[i][j]= max(dep[i][j],max(dep[i-1][j],dep[i][j-1]));
}
}
cout<<dep[n][m]<<endl;
return 0;
}
ans = -1e9;
for(int j =0;j<=m;j++){
tab[j][0][1]=-1e9;
tab[j][0][0]=-1e9;
tab[j][1][0]=-1e9;
tab[j][1][1]=-1e9;
}
for(int i =1;i<=n;i++){
for(int j =0;j<=m;j++){
dp[j][0][1]=-1e9;
dp[j][0][0]=-1e9;
dp[j][1][0]=-1e9;
dp[j][1][1]=-1e9;
}
tab[0][0][0]=0;
dp[0][0][0]=0;
for(int j =1;j<=m;j++){
if(s[i]==t[j]){
dp[j][0][0]=max(max(tab[j-1][0][0],tab[j-1][1][0]),max(tab[j-1][0][1],tab[j-1][1][1]))+arr[s[i]];
}
dp[j][1][0]= max(a+b+tab[j][0][0],b+tab[j][1][0]);
dp[j][0][1]= max(a+b+dp[j-1][0][0],b+dp [j-1][0][1]);
dp[j][1][1]= max(max(tab[j][0][1]+a+b,dp[j-1][1][0]+a+b),max(tab[j][1][1]+b,dp[j-1][1][1]+b));
}
ans = max(ans,max(max(dp[m][0][0],dp[m][1][0]),max(dp[m][0][1],dp[m][1][1])));
for(int j=0;j<=m;j++){
tab[j][0][0]=dp[j][0][0];
tab[j][1][0]=dp[j][1][0];
tab[j][0][1]=dp[j][0][1];
tab[j][1][1]=dp[j][1][1];
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
13 ms |
512 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 |
7 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 |
9 ms |
384 KB |
Output is correct |
10 |
Correct |
14 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
7 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 |
512 KB |
Output is correct |
7 |
Correct |
10 ms |
512 KB |
Output is correct |
8 |
Correct |
12 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
15 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
29952 KB |
Output is correct |
2 |
Correct |
21 ms |
18176 KB |
Output is correct |
3 |
Correct |
98 ms |
79864 KB |
Output is correct |
4 |
Correct |
206 ms |
174456 KB |
Output is correct |
5 |
Correct |
65 ms |
51448 KB |
Output is correct |
6 |
Correct |
95 ms |
76280 KB |
Output is correct |
7 |
Correct |
172 ms |
143480 KB |
Output is correct |
8 |
Correct |
64 ms |
54904 KB |
Output is correct |
9 |
Correct |
112 ms |
91384 KB |
Output is correct |
10 |
Correct |
247 ms |
196088 KB |
Output is correct |
11 |
Correct |
242 ms |
195552 KB |
Output is correct |
12 |
Correct |
246 ms |
196216 KB |
Output is correct |
13 |
Correct |
246 ms |
196088 KB |
Output is correct |
14 |
Correct |
205 ms |
196088 KB |
Output is correct |
15 |
Correct |
208 ms |
196048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
29952 KB |
Output is correct |
2 |
Correct |
21 ms |
18176 KB |
Output is correct |
3 |
Correct |
98 ms |
79864 KB |
Output is correct |
4 |
Correct |
206 ms |
174456 KB |
Output is correct |
5 |
Correct |
65 ms |
51448 KB |
Output is correct |
6 |
Correct |
95 ms |
76280 KB |
Output is correct |
7 |
Correct |
172 ms |
143480 KB |
Output is correct |
8 |
Correct |
64 ms |
54904 KB |
Output is correct |
9 |
Correct |
112 ms |
91384 KB |
Output is correct |
10 |
Correct |
247 ms |
196088 KB |
Output is correct |
11 |
Correct |
242 ms |
195552 KB |
Output is correct |
12 |
Correct |
246 ms |
196216 KB |
Output is correct |
13 |
Correct |
246 ms |
196088 KB |
Output is correct |
14 |
Correct |
205 ms |
196088 KB |
Output is correct |
15 |
Correct |
208 ms |
196048 KB |
Output is correct |
16 |
Correct |
128 ms |
1016 KB |
Output is correct |
17 |
Correct |
143 ms |
896 KB |
Output is correct |
18 |
Correct |
126 ms |
896 KB |
Output is correct |
19 |
Correct |
32 ms |
768 KB |
Output is correct |
20 |
Correct |
114 ms |
864 KB |
Output is correct |
21 |
Correct |
10 ms |
384 KB |
Output is correct |
22 |
Correct |
33 ms |
896 KB |
Output is correct |
23 |
Correct |
62 ms |
912 KB |
Output is correct |
24 |
Correct |
26 ms |
512 KB |
Output is correct |
25 |
Correct |
259 ms |
1272 KB |
Output is correct |
26 |
Correct |
258 ms |
1272 KB |
Output is correct |
27 |
Correct |
277 ms |
1152 KB |
Output is correct |
28 |
Correct |
224 ms |
1400 KB |
Output is correct |
29 |
Correct |
277 ms |
1152 KB |
Output is correct |
30 |
Correct |
252 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
29952 KB |
Output is correct |
2 |
Correct |
21 ms |
18176 KB |
Output is correct |
3 |
Correct |
98 ms |
79864 KB |
Output is correct |
4 |
Correct |
206 ms |
174456 KB |
Output is correct |
5 |
Correct |
65 ms |
51448 KB |
Output is correct |
6 |
Correct |
95 ms |
76280 KB |
Output is correct |
7 |
Correct |
172 ms |
143480 KB |
Output is correct |
8 |
Correct |
64 ms |
54904 KB |
Output is correct |
9 |
Correct |
112 ms |
91384 KB |
Output is correct |
10 |
Correct |
247 ms |
196088 KB |
Output is correct |
11 |
Correct |
242 ms |
195552 KB |
Output is correct |
12 |
Correct |
246 ms |
196216 KB |
Output is correct |
13 |
Correct |
246 ms |
196088 KB |
Output is correct |
14 |
Correct |
205 ms |
196088 KB |
Output is correct |
15 |
Correct |
208 ms |
196048 KB |
Output is correct |
16 |
Correct |
256 ms |
1024 KB |
Output is correct |
17 |
Correct |
121 ms |
640 KB |
Output is correct |
18 |
Correct |
37 ms |
768 KB |
Output is correct |
19 |
Correct |
12 ms |
512 KB |
Output is correct |
20 |
Correct |
9 ms |
640 KB |
Output is correct |
21 |
Correct |
11 ms |
1024 KB |
Output is correct |
22 |
Correct |
108 ms |
896 KB |
Output is correct |
23 |
Correct |
20 ms |
512 KB |
Output is correct |
24 |
Correct |
210 ms |
896 KB |
Output is correct |
25 |
Correct |
275 ms |
1152 KB |
Output is correct |
26 |
Correct |
307 ms |
1272 KB |
Output is correct |
27 |
Correct |
286 ms |
1272 KB |
Output is correct |
28 |
Correct |
229 ms |
1152 KB |
Output is correct |
29 |
Correct |
258 ms |
1152 KB |
Output is correct |
30 |
Correct |
257 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 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 |
5 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 |
13 ms |
512 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 |
7 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 |
9 ms |
384 KB |
Output is correct |
10 |
Correct |
14 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
512 KB |
Output is correct |
17 |
Correct |
10 ms |
512 KB |
Output is correct |
18 |
Correct |
12 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
15 ms |
512 KB |
Output is correct |
21 |
Correct |
35 ms |
29952 KB |
Output is correct |
22 |
Correct |
21 ms |
18176 KB |
Output is correct |
23 |
Correct |
98 ms |
79864 KB |
Output is correct |
24 |
Correct |
206 ms |
174456 KB |
Output is correct |
25 |
Correct |
65 ms |
51448 KB |
Output is correct |
26 |
Correct |
95 ms |
76280 KB |
Output is correct |
27 |
Correct |
172 ms |
143480 KB |
Output is correct |
28 |
Correct |
64 ms |
54904 KB |
Output is correct |
29 |
Correct |
112 ms |
91384 KB |
Output is correct |
30 |
Correct |
247 ms |
196088 KB |
Output is correct |
31 |
Correct |
242 ms |
195552 KB |
Output is correct |
32 |
Correct |
246 ms |
196216 KB |
Output is correct |
33 |
Correct |
246 ms |
196088 KB |
Output is correct |
34 |
Correct |
205 ms |
196088 KB |
Output is correct |
35 |
Correct |
208 ms |
196048 KB |
Output is correct |
36 |
Correct |
128 ms |
1016 KB |
Output is correct |
37 |
Correct |
143 ms |
896 KB |
Output is correct |
38 |
Correct |
126 ms |
896 KB |
Output is correct |
39 |
Correct |
32 ms |
768 KB |
Output is correct |
40 |
Correct |
114 ms |
864 KB |
Output is correct |
41 |
Correct |
10 ms |
384 KB |
Output is correct |
42 |
Correct |
33 ms |
896 KB |
Output is correct |
43 |
Correct |
62 ms |
912 KB |
Output is correct |
44 |
Correct |
26 ms |
512 KB |
Output is correct |
45 |
Correct |
259 ms |
1272 KB |
Output is correct |
46 |
Correct |
258 ms |
1272 KB |
Output is correct |
47 |
Correct |
277 ms |
1152 KB |
Output is correct |
48 |
Correct |
224 ms |
1400 KB |
Output is correct |
49 |
Correct |
277 ms |
1152 KB |
Output is correct |
50 |
Correct |
252 ms |
1272 KB |
Output is correct |
51 |
Correct |
256 ms |
1024 KB |
Output is correct |
52 |
Correct |
121 ms |
640 KB |
Output is correct |
53 |
Correct |
37 ms |
768 KB |
Output is correct |
54 |
Correct |
12 ms |
512 KB |
Output is correct |
55 |
Correct |
9 ms |
640 KB |
Output is correct |
56 |
Correct |
11 ms |
1024 KB |
Output is correct |
57 |
Correct |
108 ms |
896 KB |
Output is correct |
58 |
Correct |
20 ms |
512 KB |
Output is correct |
59 |
Correct |
210 ms |
896 KB |
Output is correct |
60 |
Correct |
275 ms |
1152 KB |
Output is correct |
61 |
Correct |
307 ms |
1272 KB |
Output is correct |
62 |
Correct |
286 ms |
1272 KB |
Output is correct |
63 |
Correct |
229 ms |
1152 KB |
Output is correct |
64 |
Correct |
258 ms |
1152 KB |
Output is correct |
65 |
Correct |
257 ms |
1272 KB |
Output is correct |
66 |
Correct |
5 ms |
384 KB |
Output is correct |
67 |
Correct |
4 ms |
384 KB |
Output is correct |
68 |
Correct |
4 ms |
384 KB |
Output is correct |
69 |
Correct |
5 ms |
384 KB |
Output is correct |
70 |
Correct |
5 ms |
384 KB |
Output is correct |
71 |
Correct |
4 ms |
384 KB |
Output is correct |
72 |
Correct |
5 ms |
384 KB |
Output is correct |
73 |
Correct |
4 ms |
384 KB |
Output is correct |
74 |
Correct |
4 ms |
384 KB |
Output is correct |
75 |
Correct |
5 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 |
5 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 |
181 ms |
1068 KB |
Output is correct |
82 |
Correct |
56 ms |
640 KB |
Output is correct |
83 |
Correct |
83 ms |
896 KB |
Output is correct |
84 |
Correct |
47 ms |
896 KB |
Output is correct |
85 |
Correct |
79 ms |
888 KB |
Output is correct |
86 |
Correct |
66 ms |
768 KB |
Output is correct |
87 |
Correct |
15 ms |
640 KB |
Output is correct |
88 |
Correct |
12 ms |
384 KB |
Output is correct |
89 |
Correct |
34 ms |
640 KB |
Output is correct |
90 |
Correct |
337 ms |
1272 KB |
Output is correct |
91 |
Correct |
227 ms |
1152 KB |
Output is correct |
92 |
Correct |
235 ms |
1272 KB |
Output is correct |
93 |
Correct |
231 ms |
1272 KB |
Output is correct |
94 |
Correct |
244 ms |
1272 KB |
Output is correct |
95 |
Correct |
227 ms |
1152 KB |
Output is correct |
96 |
Correct |
232 ms |
1152 KB |
Output is correct |
97 |
Correct |
238 ms |
1152 KB |
Output is correct |
98 |
Correct |
255 ms |
1272 KB |
Output is correct |
99 |
Correct |
262 ms |
1272 KB |
Output is correct |
100 |
Correct |
263 ms |
1272 KB |
Output is correct |