#include<bits/stdc++.h>
using namespace std;
// struct FenwickTree2d{
// int n, m;
// vector<vector<int>> t;
// void init(int _n, int _m){
// n=_n+1; m=_m+1;
// vector<vector<int>>(n+1, vector<int>(m+1, -1e9)).swap(t);
// }
// void update(int x, int y, int val){
// ++x; ++y;
// for (int i=x; i<=n; i+=i&(-i)) for (int j=y; j<=m; j+=j&(-j)) t[i][j]=max(t[i][j], val);
// }
// int get(int x, int y){
// ++x; ++y;
// int ans=-1e9;
// for (int i=x; i; i-=i&(-i)) for (int j=y; j; j-=j&(-j)) ans=max(ans, t[i][j]);
// return ans;
// }
// } bit2d;
const int N=5001;
int n, m, A, B, k, v[N], s[N], t[N], f[2][N], r[2][N], c[N][N], mx[N][N];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
for (int i=0; i<2; ++i) for (int j=0; j<N; ++j) f[i][j]=-1e9, r[i][j]=-1e9;
for (int i=0; i<N; ++i) for (int j=0; j<N; ++j) c[i][j]=mx[i][j]=-1e9;
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=1; i<=m; ++i) cin >> t[i];
// bit2d.init(m, n);
for (int j=0; j<=n; ++j){
f[0][j]=0;
r[0][j]=f[0][j]-B*j;
c[0][j]=f[0][j];
mx[0][j]=-B*j;
// bit2d.update(0, j, -B*j);
}
for (int i=0; i<2; ++i) for (int j=1; j<=n; ++j){
r[i][j]=max(r[i][j], r[i][j-1]);
}
for (int i=1; i<=m; ++i) for (int j=0; j<=n; ++j){
c[i][j]=max(c[i][j], c[i-1][j]);
}
for (int i=1; i<=n; ++i) mx[0][i]=max(mx[0][i], mx[0][i-1]);
for (int i=1; i<=m; ++i) mx[i][0]=max(mx[i][0], mx[i-1][0]);
int ans=A+B*m;
for (int i=1; i<=m; ++i){
for (int j=1; j<=n; ++j){
if (s[j]==t[i]){
if (i>=2 && j>=2) f[1][j]=max(f[1][j], mx[i-2][j-2]+A+A+B*(i-1)+B*(j-1)+v[t[i]]);
if (i>=2) f[1][j]=max(f[1][j], c[i-2][j-1]+A+B*(i-1)+v[t[i]]);
if (j>=2) f[1][j]=max(f[1][j], r[0][j-2]+A+B*(j-1)+v[t[i]]);
f[1][j]=max(f[1][j], f[0][j-1]+v[t[i]]);
mx[i][j]=max(mx[i][j], f[1][j]-B*i-B*j);
if (i!=m) ans=max(ans, f[1][j]+A+B*(m-i));
else ans=max(ans, f[1][j]);
}
mx[i][j]=max({mx[i][j], mx[i-1][j], mx[i][j-1]});
r[1][j]=max({r[1][j], r[1][j-1], f[1][j]-B*j});
c[i][j]=max({c[i][j], c[i-1][j], f[1][j]-B*i});
}
memcpy(f[0], f[1], sizeof f[1]);
memcpy(r[0], r[1], sizeof r[1]);
for (int j=0; j<=n; ++j) f[1][j]=-1e9, r[1][j]=-1e9;
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
196176 KB |
Output is correct |
2 |
Correct |
48 ms |
196300 KB |
Output is correct |
3 |
Correct |
35 ms |
196220 KB |
Output is correct |
4 |
Correct |
36 ms |
196184 KB |
Output is correct |
5 |
Correct |
35 ms |
196184 KB |
Output is correct |
6 |
Correct |
38 ms |
196180 KB |
Output is correct |
7 |
Correct |
35 ms |
196176 KB |
Output is correct |
8 |
Correct |
37 ms |
196180 KB |
Output is correct |
9 |
Correct |
41 ms |
196184 KB |
Output is correct |
10 |
Correct |
47 ms |
196176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
196256 KB |
Output is correct |
2 |
Correct |
35 ms |
196184 KB |
Output is correct |
3 |
Correct |
38 ms |
196188 KB |
Output is correct |
4 |
Correct |
35 ms |
196184 KB |
Output is correct |
5 |
Correct |
37 ms |
196188 KB |
Output is correct |
6 |
Correct |
41 ms |
196116 KB |
Output is correct |
7 |
Correct |
42 ms |
196180 KB |
Output is correct |
8 |
Correct |
44 ms |
196308 KB |
Output is correct |
9 |
Correct |
36 ms |
196132 KB |
Output is correct |
10 |
Correct |
49 ms |
196180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
196288 KB |
Output is correct |
2 |
Correct |
40 ms |
196092 KB |
Output is correct |
3 |
Correct |
83 ms |
196180 KB |
Output is correct |
4 |
Correct |
154 ms |
196364 KB |
Output is correct |
5 |
Correct |
81 ms |
196328 KB |
Output is correct |
6 |
Correct |
87 ms |
196264 KB |
Output is correct |
7 |
Correct |
129 ms |
196180 KB |
Output is correct |
8 |
Correct |
66 ms |
196340 KB |
Output is correct |
9 |
Correct |
100 ms |
196604 KB |
Output is correct |
10 |
Correct |
178 ms |
196364 KB |
Output is correct |
11 |
Correct |
178 ms |
196368 KB |
Output is correct |
12 |
Correct |
183 ms |
196368 KB |
Output is correct |
13 |
Correct |
178 ms |
196176 KB |
Output is correct |
14 |
Correct |
319 ms |
196348 KB |
Output is correct |
15 |
Correct |
317 ms |
196252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
196288 KB |
Output is correct |
2 |
Correct |
40 ms |
196092 KB |
Output is correct |
3 |
Correct |
83 ms |
196180 KB |
Output is correct |
4 |
Correct |
154 ms |
196364 KB |
Output is correct |
5 |
Correct |
81 ms |
196328 KB |
Output is correct |
6 |
Correct |
87 ms |
196264 KB |
Output is correct |
7 |
Correct |
129 ms |
196180 KB |
Output is correct |
8 |
Correct |
66 ms |
196340 KB |
Output is correct |
9 |
Correct |
100 ms |
196604 KB |
Output is correct |
10 |
Correct |
178 ms |
196364 KB |
Output is correct |
11 |
Correct |
178 ms |
196368 KB |
Output is correct |
12 |
Correct |
183 ms |
196368 KB |
Output is correct |
13 |
Correct |
178 ms |
196176 KB |
Output is correct |
14 |
Correct |
319 ms |
196348 KB |
Output is correct |
15 |
Correct |
317 ms |
196252 KB |
Output is correct |
16 |
Correct |
112 ms |
196356 KB |
Output is correct |
17 |
Correct |
130 ms |
196352 KB |
Output is correct |
18 |
Correct |
115 ms |
196188 KB |
Output is correct |
19 |
Correct |
54 ms |
196176 KB |
Output is correct |
20 |
Correct |
95 ms |
196340 KB |
Output is correct |
21 |
Correct |
39 ms |
196180 KB |
Output is correct |
22 |
Correct |
58 ms |
196432 KB |
Output is correct |
23 |
Correct |
76 ms |
196176 KB |
Output is correct |
24 |
Correct |
50 ms |
196180 KB |
Output is correct |
25 |
Correct |
178 ms |
196372 KB |
Output is correct |
26 |
Correct |
183 ms |
196360 KB |
Output is correct |
27 |
Correct |
180 ms |
196180 KB |
Output is correct |
28 |
Correct |
177 ms |
196176 KB |
Output is correct |
29 |
Correct |
325 ms |
196348 KB |
Output is correct |
30 |
Correct |
316 ms |
196180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
196288 KB |
Output is correct |
2 |
Correct |
40 ms |
196092 KB |
Output is correct |
3 |
Correct |
83 ms |
196180 KB |
Output is correct |
4 |
Correct |
154 ms |
196364 KB |
Output is correct |
5 |
Correct |
81 ms |
196328 KB |
Output is correct |
6 |
Correct |
87 ms |
196264 KB |
Output is correct |
7 |
Correct |
129 ms |
196180 KB |
Output is correct |
8 |
Correct |
66 ms |
196340 KB |
Output is correct |
9 |
Correct |
100 ms |
196604 KB |
Output is correct |
10 |
Correct |
178 ms |
196364 KB |
Output is correct |
11 |
Correct |
178 ms |
196368 KB |
Output is correct |
12 |
Correct |
183 ms |
196368 KB |
Output is correct |
13 |
Correct |
178 ms |
196176 KB |
Output is correct |
14 |
Correct |
319 ms |
196348 KB |
Output is correct |
15 |
Correct |
317 ms |
196252 KB |
Output is correct |
16 |
Correct |
156 ms |
196352 KB |
Output is correct |
17 |
Correct |
98 ms |
196096 KB |
Output is correct |
18 |
Correct |
54 ms |
196176 KB |
Output is correct |
19 |
Correct |
40 ms |
196180 KB |
Output is correct |
20 |
Correct |
39 ms |
196084 KB |
Output is correct |
21 |
Correct |
43 ms |
196184 KB |
Output is correct |
22 |
Correct |
92 ms |
196244 KB |
Output is correct |
23 |
Correct |
46 ms |
196176 KB |
Output is correct |
24 |
Correct |
134 ms |
196352 KB |
Output is correct |
25 |
Correct |
181 ms |
196372 KB |
Output is correct |
26 |
Correct |
180 ms |
196360 KB |
Output is correct |
27 |
Correct |
179 ms |
196372 KB |
Output is correct |
28 |
Correct |
176 ms |
196360 KB |
Output is correct |
29 |
Correct |
326 ms |
196548 KB |
Output is correct |
30 |
Correct |
318 ms |
196348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
196256 KB |
Output is correct |
2 |
Correct |
35 ms |
196176 KB |
Output is correct |
3 |
Correct |
36 ms |
196188 KB |
Output is correct |
4 |
Correct |
36 ms |
196244 KB |
Output is correct |
5 |
Correct |
35 ms |
196180 KB |
Output is correct |
6 |
Correct |
36 ms |
196180 KB |
Output is correct |
7 |
Correct |
35 ms |
196100 KB |
Output is correct |
8 |
Correct |
35 ms |
196184 KB |
Output is correct |
9 |
Correct |
35 ms |
196180 KB |
Output is correct |
10 |
Correct |
35 ms |
196180 KB |
Output is correct |
11 |
Correct |
37 ms |
196244 KB |
Output is correct |
12 |
Correct |
35 ms |
196176 KB |
Output is correct |
13 |
Correct |
35 ms |
196176 KB |
Output is correct |
14 |
Correct |
35 ms |
196176 KB |
Output is correct |
15 |
Correct |
36 ms |
196184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
196176 KB |
Output is correct |
2 |
Correct |
48 ms |
196300 KB |
Output is correct |
3 |
Correct |
35 ms |
196220 KB |
Output is correct |
4 |
Correct |
36 ms |
196184 KB |
Output is correct |
5 |
Correct |
35 ms |
196184 KB |
Output is correct |
6 |
Correct |
38 ms |
196180 KB |
Output is correct |
7 |
Correct |
35 ms |
196176 KB |
Output is correct |
8 |
Correct |
37 ms |
196180 KB |
Output is correct |
9 |
Correct |
41 ms |
196184 KB |
Output is correct |
10 |
Correct |
47 ms |
196176 KB |
Output is correct |
11 |
Correct |
36 ms |
196256 KB |
Output is correct |
12 |
Correct |
35 ms |
196184 KB |
Output is correct |
13 |
Correct |
38 ms |
196188 KB |
Output is correct |
14 |
Correct |
35 ms |
196184 KB |
Output is correct |
15 |
Correct |
37 ms |
196188 KB |
Output is correct |
16 |
Correct |
41 ms |
196116 KB |
Output is correct |
17 |
Correct |
42 ms |
196180 KB |
Output is correct |
18 |
Correct |
44 ms |
196308 KB |
Output is correct |
19 |
Correct |
36 ms |
196132 KB |
Output is correct |
20 |
Correct |
49 ms |
196180 KB |
Output is correct |
21 |
Correct |
50 ms |
196288 KB |
Output is correct |
22 |
Correct |
40 ms |
196092 KB |
Output is correct |
23 |
Correct |
83 ms |
196180 KB |
Output is correct |
24 |
Correct |
154 ms |
196364 KB |
Output is correct |
25 |
Correct |
81 ms |
196328 KB |
Output is correct |
26 |
Correct |
87 ms |
196264 KB |
Output is correct |
27 |
Correct |
129 ms |
196180 KB |
Output is correct |
28 |
Correct |
66 ms |
196340 KB |
Output is correct |
29 |
Correct |
100 ms |
196604 KB |
Output is correct |
30 |
Correct |
178 ms |
196364 KB |
Output is correct |
31 |
Correct |
178 ms |
196368 KB |
Output is correct |
32 |
Correct |
183 ms |
196368 KB |
Output is correct |
33 |
Correct |
178 ms |
196176 KB |
Output is correct |
34 |
Correct |
319 ms |
196348 KB |
Output is correct |
35 |
Correct |
317 ms |
196252 KB |
Output is correct |
36 |
Correct |
112 ms |
196356 KB |
Output is correct |
37 |
Correct |
130 ms |
196352 KB |
Output is correct |
38 |
Correct |
115 ms |
196188 KB |
Output is correct |
39 |
Correct |
54 ms |
196176 KB |
Output is correct |
40 |
Correct |
95 ms |
196340 KB |
Output is correct |
41 |
Correct |
39 ms |
196180 KB |
Output is correct |
42 |
Correct |
58 ms |
196432 KB |
Output is correct |
43 |
Correct |
76 ms |
196176 KB |
Output is correct |
44 |
Correct |
50 ms |
196180 KB |
Output is correct |
45 |
Correct |
178 ms |
196372 KB |
Output is correct |
46 |
Correct |
183 ms |
196360 KB |
Output is correct |
47 |
Correct |
180 ms |
196180 KB |
Output is correct |
48 |
Correct |
177 ms |
196176 KB |
Output is correct |
49 |
Correct |
325 ms |
196348 KB |
Output is correct |
50 |
Correct |
316 ms |
196180 KB |
Output is correct |
51 |
Correct |
156 ms |
196352 KB |
Output is correct |
52 |
Correct |
98 ms |
196096 KB |
Output is correct |
53 |
Correct |
54 ms |
196176 KB |
Output is correct |
54 |
Correct |
40 ms |
196180 KB |
Output is correct |
55 |
Correct |
39 ms |
196084 KB |
Output is correct |
56 |
Correct |
43 ms |
196184 KB |
Output is correct |
57 |
Correct |
92 ms |
196244 KB |
Output is correct |
58 |
Correct |
46 ms |
196176 KB |
Output is correct |
59 |
Correct |
134 ms |
196352 KB |
Output is correct |
60 |
Correct |
181 ms |
196372 KB |
Output is correct |
61 |
Correct |
180 ms |
196360 KB |
Output is correct |
62 |
Correct |
179 ms |
196372 KB |
Output is correct |
63 |
Correct |
176 ms |
196360 KB |
Output is correct |
64 |
Correct |
326 ms |
196548 KB |
Output is correct |
65 |
Correct |
318 ms |
196348 KB |
Output is correct |
66 |
Correct |
35 ms |
196256 KB |
Output is correct |
67 |
Correct |
35 ms |
196176 KB |
Output is correct |
68 |
Correct |
36 ms |
196188 KB |
Output is correct |
69 |
Correct |
36 ms |
196244 KB |
Output is correct |
70 |
Correct |
35 ms |
196180 KB |
Output is correct |
71 |
Correct |
36 ms |
196180 KB |
Output is correct |
72 |
Correct |
35 ms |
196100 KB |
Output is correct |
73 |
Correct |
35 ms |
196184 KB |
Output is correct |
74 |
Correct |
35 ms |
196180 KB |
Output is correct |
75 |
Correct |
35 ms |
196180 KB |
Output is correct |
76 |
Correct |
37 ms |
196244 KB |
Output is correct |
77 |
Correct |
35 ms |
196176 KB |
Output is correct |
78 |
Correct |
35 ms |
196176 KB |
Output is correct |
79 |
Correct |
35 ms |
196176 KB |
Output is correct |
80 |
Correct |
36 ms |
196184 KB |
Output is correct |
81 |
Correct |
150 ms |
196372 KB |
Output is correct |
82 |
Correct |
64 ms |
196180 KB |
Output is correct |
83 |
Correct |
89 ms |
196176 KB |
Output is correct |
84 |
Correct |
66 ms |
196180 KB |
Output is correct |
85 |
Correct |
87 ms |
196340 KB |
Output is correct |
86 |
Correct |
75 ms |
196180 KB |
Output is correct |
87 |
Correct |
43 ms |
196180 KB |
Output is correct |
88 |
Correct |
40 ms |
196180 KB |
Output is correct |
89 |
Correct |
55 ms |
196088 KB |
Output is correct |
90 |
Correct |
196 ms |
196176 KB |
Output is correct |
91 |
Correct |
181 ms |
196340 KB |
Output is correct |
92 |
Correct |
179 ms |
196180 KB |
Output is correct |
93 |
Correct |
178 ms |
196364 KB |
Output is correct |
94 |
Correct |
182 ms |
196364 KB |
Output is correct |
95 |
Correct |
178 ms |
196184 KB |
Output is correct |
96 |
Correct |
180 ms |
196356 KB |
Output is correct |
97 |
Correct |
184 ms |
196356 KB |
Output is correct |
98 |
Correct |
317 ms |
196180 KB |
Output is correct |
99 |
Correct |
318 ms |
196176 KB |
Output is correct |
100 |
Correct |
319 ms |
196352 KB |
Output is correct |