#include <bits/stdc++.h>
using namespace std;
typedef long long int li;
typedef pair<int,int> pii;
typedef pair<li,li> pll;
#define mp make_pair
#define pb push_back
int k, n, m, a, b;
int v[1005], s[10005], t[10005];
int dp[3][10005]; //max happiness -idx*b
int dp1[3][10005], dp2[3][10005], dp3[3][10005];
int ans = 0;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n >> m >> a >> b;
a = -a;
b = -b;
for (int i=0; i<k; i++){
cin >> v[i+1];
}
for (int i=2; i<n+2; i++){
cin >> s[i];
}
for (int i=2; i<m+2; i++){
cin >> t[i];
}
for (int i=0; i<2; i++){
for (int j=0; j<m+2; j++){
dp[i][j] = 2*b;
dp1[i][j] = dp2[i][j] = dp3[i][j] = -1e8;
}
}
ans = -(a + m*b);
for (int i=2; i<n+2; i++){
int x = i%3, y = (i+1)%3;
for (int j=2; j<m+2; j++){
dp[x][j] = 2*b;
dp1[x][j] = dp2[x][j] = dp3[x][j] = -1e8;
if (s[i] == t[j]){
dp3[x][j] = v[s[i]] - (j-2)*b + (j+1)*b + (i+1)*b - a;
if (j==2) dp3[x][j] += a;
dp3[x][j] = max(dp3[x][j], max(dp3[(i+2)%3][j-1], dp[y][j-2] - 2*a) + v[s[i]] + 2*b);
dp3[x][j] = max(dp3[x][j], max(dp1[(i+2)%3][j-1], dp2[(i+2)%3][j-1]) + v[s[i]] - a + 2*b);
}
dp1[x][j] = max(dp1[x][j-1], dp3[x][j]);
dp2[x][j] = max(dp2[(i+2)%3][j], dp3[x][j]);
dp[x][j] = max(dp[x][j],max(dp3[x][j],max(dp[(i+2)%3][j], dp[x][j-1])));
int tt = dp3[x][j] - (m+2)*b - (i+1)*b - a;
if (j==m+1) tt += a;
ans = max(ans, tt);
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
14 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 |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
7 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
7 ms |
380 KB |
Output is correct |
9 |
Correct |
9 ms |
504 KB |
Output is correct |
10 |
Correct |
14 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
7 ms |
408 KB |
Output is correct |
4 |
Correct |
6 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
10 ms |
504 KB |
Output is correct |
7 |
Correct |
16 ms |
504 KB |
Output is correct |
8 |
Correct |
11 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
13 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
556 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
57 ms |
504 KB |
Output is correct |
4 |
Correct |
138 ms |
632 KB |
Output is correct |
5 |
Correct |
42 ms |
632 KB |
Output is correct |
6 |
Correct |
59 ms |
632 KB |
Output is correct |
7 |
Correct |
106 ms |
632 KB |
Output is correct |
8 |
Correct |
38 ms |
504 KB |
Output is correct |
9 |
Correct |
70 ms |
632 KB |
Output is correct |
10 |
Correct |
162 ms |
724 KB |
Output is correct |
11 |
Correct |
200 ms |
720 KB |
Output is correct |
12 |
Correct |
166 ms |
632 KB |
Output is correct |
13 |
Correct |
164 ms |
632 KB |
Output is correct |
14 |
Correct |
241 ms |
632 KB |
Output is correct |
15 |
Correct |
239 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
556 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
57 ms |
504 KB |
Output is correct |
4 |
Correct |
138 ms |
632 KB |
Output is correct |
5 |
Correct |
42 ms |
632 KB |
Output is correct |
6 |
Correct |
59 ms |
632 KB |
Output is correct |
7 |
Correct |
106 ms |
632 KB |
Output is correct |
8 |
Correct |
38 ms |
504 KB |
Output is correct |
9 |
Correct |
70 ms |
632 KB |
Output is correct |
10 |
Correct |
162 ms |
724 KB |
Output is correct |
11 |
Correct |
200 ms |
720 KB |
Output is correct |
12 |
Correct |
166 ms |
632 KB |
Output is correct |
13 |
Correct |
164 ms |
632 KB |
Output is correct |
14 |
Correct |
241 ms |
632 KB |
Output is correct |
15 |
Correct |
239 ms |
632 KB |
Output is correct |
16 |
Correct |
88 ms |
632 KB |
Output is correct |
17 |
Correct |
106 ms |
632 KB |
Output is correct |
18 |
Correct |
97 ms |
608 KB |
Output is correct |
19 |
Correct |
24 ms |
504 KB |
Output is correct |
20 |
Correct |
67 ms |
504 KB |
Output is correct |
21 |
Correct |
9 ms |
376 KB |
Output is correct |
22 |
Correct |
25 ms |
504 KB |
Output is correct |
23 |
Correct |
69 ms |
632 KB |
Output is correct |
24 |
Correct |
20 ms |
504 KB |
Output is correct |
25 |
Correct |
182 ms |
632 KB |
Output is correct |
26 |
Correct |
164 ms |
716 KB |
Output is correct |
27 |
Correct |
166 ms |
632 KB |
Output is correct |
28 |
Correct |
164 ms |
728 KB |
Output is correct |
29 |
Correct |
232 ms |
632 KB |
Output is correct |
30 |
Correct |
239 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
556 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
57 ms |
504 KB |
Output is correct |
4 |
Correct |
138 ms |
632 KB |
Output is correct |
5 |
Correct |
42 ms |
632 KB |
Output is correct |
6 |
Correct |
59 ms |
632 KB |
Output is correct |
7 |
Correct |
106 ms |
632 KB |
Output is correct |
8 |
Correct |
38 ms |
504 KB |
Output is correct |
9 |
Correct |
70 ms |
632 KB |
Output is correct |
10 |
Correct |
162 ms |
724 KB |
Output is correct |
11 |
Correct |
200 ms |
720 KB |
Output is correct |
12 |
Correct |
166 ms |
632 KB |
Output is correct |
13 |
Correct |
164 ms |
632 KB |
Output is correct |
14 |
Correct |
241 ms |
632 KB |
Output is correct |
15 |
Correct |
239 ms |
632 KB |
Output is correct |
16 |
Correct |
150 ms |
632 KB |
Output is correct |
17 |
Correct |
66 ms |
504 KB |
Output is correct |
18 |
Correct |
36 ms |
504 KB |
Output is correct |
19 |
Correct |
10 ms |
376 KB |
Output is correct |
20 |
Correct |
7 ms |
504 KB |
Output is correct |
21 |
Correct |
9 ms |
632 KB |
Output is correct |
22 |
Correct |
67 ms |
504 KB |
Output is correct |
23 |
Correct |
16 ms |
376 KB |
Output is correct |
24 |
Correct |
115 ms |
632 KB |
Output is correct |
25 |
Correct |
163 ms |
720 KB |
Output is correct |
26 |
Correct |
162 ms |
716 KB |
Output is correct |
27 |
Correct |
163 ms |
632 KB |
Output is correct |
28 |
Correct |
162 ms |
632 KB |
Output is correct |
29 |
Correct |
245 ms |
632 KB |
Output is correct |
30 |
Correct |
249 ms |
632 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 |
376 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 |
376 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 |
376 KB |
Output is correct |
2 |
Correct |
14 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 |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
7 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
7 ms |
380 KB |
Output is correct |
9 |
Correct |
9 ms |
504 KB |
Output is correct |
10 |
Correct |
14 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
7 ms |
408 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
10 ms |
504 KB |
Output is correct |
17 |
Correct |
16 ms |
504 KB |
Output is correct |
18 |
Correct |
11 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
13 ms |
376 KB |
Output is correct |
21 |
Correct |
22 ms |
556 KB |
Output is correct |
22 |
Correct |
11 ms |
376 KB |
Output is correct |
23 |
Correct |
57 ms |
504 KB |
Output is correct |
24 |
Correct |
138 ms |
632 KB |
Output is correct |
25 |
Correct |
42 ms |
632 KB |
Output is correct |
26 |
Correct |
59 ms |
632 KB |
Output is correct |
27 |
Correct |
106 ms |
632 KB |
Output is correct |
28 |
Correct |
38 ms |
504 KB |
Output is correct |
29 |
Correct |
70 ms |
632 KB |
Output is correct |
30 |
Correct |
162 ms |
724 KB |
Output is correct |
31 |
Correct |
200 ms |
720 KB |
Output is correct |
32 |
Correct |
166 ms |
632 KB |
Output is correct |
33 |
Correct |
164 ms |
632 KB |
Output is correct |
34 |
Correct |
241 ms |
632 KB |
Output is correct |
35 |
Correct |
239 ms |
632 KB |
Output is correct |
36 |
Correct |
88 ms |
632 KB |
Output is correct |
37 |
Correct |
106 ms |
632 KB |
Output is correct |
38 |
Correct |
97 ms |
608 KB |
Output is correct |
39 |
Correct |
24 ms |
504 KB |
Output is correct |
40 |
Correct |
67 ms |
504 KB |
Output is correct |
41 |
Correct |
9 ms |
376 KB |
Output is correct |
42 |
Correct |
25 ms |
504 KB |
Output is correct |
43 |
Correct |
69 ms |
632 KB |
Output is correct |
44 |
Correct |
20 ms |
504 KB |
Output is correct |
45 |
Correct |
182 ms |
632 KB |
Output is correct |
46 |
Correct |
164 ms |
716 KB |
Output is correct |
47 |
Correct |
166 ms |
632 KB |
Output is correct |
48 |
Correct |
164 ms |
728 KB |
Output is correct |
49 |
Correct |
232 ms |
632 KB |
Output is correct |
50 |
Correct |
239 ms |
632 KB |
Output is correct |
51 |
Correct |
150 ms |
632 KB |
Output is correct |
52 |
Correct |
66 ms |
504 KB |
Output is correct |
53 |
Correct |
36 ms |
504 KB |
Output is correct |
54 |
Correct |
10 ms |
376 KB |
Output is correct |
55 |
Correct |
7 ms |
504 KB |
Output is correct |
56 |
Correct |
9 ms |
632 KB |
Output is correct |
57 |
Correct |
67 ms |
504 KB |
Output is correct |
58 |
Correct |
16 ms |
376 KB |
Output is correct |
59 |
Correct |
115 ms |
632 KB |
Output is correct |
60 |
Correct |
163 ms |
720 KB |
Output is correct |
61 |
Correct |
162 ms |
716 KB |
Output is correct |
62 |
Correct |
163 ms |
632 KB |
Output is correct |
63 |
Correct |
162 ms |
632 KB |
Output is correct |
64 |
Correct |
245 ms |
632 KB |
Output is correct |
65 |
Correct |
249 ms |
632 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 |
376 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 |
376 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 |
136 ms |
632 KB |
Output is correct |
82 |
Correct |
34 ms |
504 KB |
Output is correct |
83 |
Correct |
61 ms |
632 KB |
Output is correct |
84 |
Correct |
35 ms |
504 KB |
Output is correct |
85 |
Correct |
54 ms |
504 KB |
Output is correct |
86 |
Correct |
47 ms |
504 KB |
Output is correct |
87 |
Correct |
12 ms |
504 KB |
Output is correct |
88 |
Correct |
11 ms |
376 KB |
Output is correct |
89 |
Correct |
22 ms |
504 KB |
Output is correct |
90 |
Correct |
178 ms |
716 KB |
Output is correct |
91 |
Correct |
160 ms |
632 KB |
Output is correct |
92 |
Correct |
160 ms |
632 KB |
Output is correct |
93 |
Correct |
160 ms |
636 KB |
Output is correct |
94 |
Correct |
170 ms |
632 KB |
Output is correct |
95 |
Correct |
169 ms |
632 KB |
Output is correct |
96 |
Correct |
180 ms |
628 KB |
Output is correct |
97 |
Correct |
171 ms |
632 KB |
Output is correct |
98 |
Correct |
246 ms |
636 KB |
Output is correct |
99 |
Correct |
230 ms |
632 KB |
Output is correct |
100 |
Correct |
235 ms |
632 KB |
Output is correct |