#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];
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]=-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];
bit2d.update(0, j, 0);
}
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]);
}
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], bit2d.get(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]]);
bit2d.update(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]);
}
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 |
30 ms |
98416 KB |
Output is correct |
2 |
Correct |
82 ms |
101780 KB |
Output is correct |
3 |
Correct |
19 ms |
98380 KB |
Output is correct |
4 |
Correct |
20 ms |
98392 KB |
Output is correct |
5 |
Correct |
18 ms |
98396 KB |
Output is correct |
6 |
Correct |
36 ms |
99164 KB |
Output is correct |
7 |
Correct |
20 ms |
98244 KB |
Output is correct |
8 |
Correct |
24 ms |
98668 KB |
Output is correct |
9 |
Correct |
53 ms |
100188 KB |
Output is correct |
10 |
Correct |
97 ms |
102384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
98396 KB |
Output is correct |
2 |
Correct |
18 ms |
98376 KB |
Output is correct |
3 |
Correct |
35 ms |
99164 KB |
Output is correct |
4 |
Correct |
21 ms |
98396 KB |
Output is correct |
5 |
Correct |
23 ms |
98644 KB |
Output is correct |
6 |
Correct |
57 ms |
100308 KB |
Output is correct |
7 |
Correct |
55 ms |
100176 KB |
Output is correct |
8 |
Correct |
71 ms |
100956 KB |
Output is correct |
9 |
Correct |
23 ms |
98540 KB |
Output is correct |
10 |
Correct |
92 ms |
102236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
107868 KB |
Output is correct |
2 |
Correct |
29 ms |
102416 KB |
Output is correct |
3 |
Correct |
94 ms |
130576 KB |
Output is correct |
4 |
Correct |
208 ms |
176564 KB |
Output is correct |
5 |
Correct |
81 ms |
121172 KB |
Output is correct |
6 |
Correct |
99 ms |
131672 KB |
Output is correct |
7 |
Correct |
151 ms |
160688 KB |
Output is correct |
8 |
Correct |
61 ms |
118608 KB |
Output is correct |
9 |
Correct |
100 ms |
138316 KB |
Output is correct |
10 |
Correct |
236 ms |
196124 KB |
Output is correct |
11 |
Correct |
225 ms |
195980 KB |
Output is correct |
12 |
Correct |
220 ms |
196480 KB |
Output is correct |
13 |
Correct |
201 ms |
196320 KB |
Output is correct |
14 |
Execution timed out |
2058 ms |
196184 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
107868 KB |
Output is correct |
2 |
Correct |
29 ms |
102416 KB |
Output is correct |
3 |
Correct |
94 ms |
130576 KB |
Output is correct |
4 |
Correct |
208 ms |
176564 KB |
Output is correct |
5 |
Correct |
81 ms |
121172 KB |
Output is correct |
6 |
Correct |
99 ms |
131672 KB |
Output is correct |
7 |
Correct |
151 ms |
160688 KB |
Output is correct |
8 |
Correct |
61 ms |
118608 KB |
Output is correct |
9 |
Correct |
100 ms |
138316 KB |
Output is correct |
10 |
Correct |
236 ms |
196124 KB |
Output is correct |
11 |
Correct |
225 ms |
195980 KB |
Output is correct |
12 |
Correct |
220 ms |
196480 KB |
Output is correct |
13 |
Correct |
201 ms |
196320 KB |
Output is correct |
14 |
Execution timed out |
2058 ms |
196184 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
107868 KB |
Output is correct |
2 |
Correct |
29 ms |
102416 KB |
Output is correct |
3 |
Correct |
94 ms |
130576 KB |
Output is correct |
4 |
Correct |
208 ms |
176564 KB |
Output is correct |
5 |
Correct |
81 ms |
121172 KB |
Output is correct |
6 |
Correct |
99 ms |
131672 KB |
Output is correct |
7 |
Correct |
151 ms |
160688 KB |
Output is correct |
8 |
Correct |
61 ms |
118608 KB |
Output is correct |
9 |
Correct |
100 ms |
138316 KB |
Output is correct |
10 |
Correct |
236 ms |
196124 KB |
Output is correct |
11 |
Correct |
225 ms |
195980 KB |
Output is correct |
12 |
Correct |
220 ms |
196480 KB |
Output is correct |
13 |
Correct |
201 ms |
196320 KB |
Output is correct |
14 |
Execution timed out |
2058 ms |
196184 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
98392 KB |
Output is correct |
2 |
Correct |
19 ms |
98396 KB |
Output is correct |
3 |
Correct |
19 ms |
98332 KB |
Output is correct |
4 |
Correct |
19 ms |
98356 KB |
Output is correct |
5 |
Correct |
22 ms |
98384 KB |
Output is correct |
6 |
Correct |
18 ms |
98408 KB |
Output is correct |
7 |
Correct |
18 ms |
98396 KB |
Output is correct |
8 |
Correct |
19 ms |
98396 KB |
Output is correct |
9 |
Correct |
18 ms |
98416 KB |
Output is correct |
10 |
Correct |
19 ms |
98396 KB |
Output is correct |
11 |
Correct |
18 ms |
98328 KB |
Output is correct |
12 |
Correct |
19 ms |
98524 KB |
Output is correct |
13 |
Correct |
19 ms |
98648 KB |
Output is correct |
14 |
Correct |
19 ms |
98396 KB |
Output is correct |
15 |
Correct |
19 ms |
98396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
98416 KB |
Output is correct |
2 |
Correct |
82 ms |
101780 KB |
Output is correct |
3 |
Correct |
19 ms |
98380 KB |
Output is correct |
4 |
Correct |
20 ms |
98392 KB |
Output is correct |
5 |
Correct |
18 ms |
98396 KB |
Output is correct |
6 |
Correct |
36 ms |
99164 KB |
Output is correct |
7 |
Correct |
20 ms |
98244 KB |
Output is correct |
8 |
Correct |
24 ms |
98668 KB |
Output is correct |
9 |
Correct |
53 ms |
100188 KB |
Output is correct |
10 |
Correct |
97 ms |
102384 KB |
Output is correct |
11 |
Correct |
20 ms |
98396 KB |
Output is correct |
12 |
Correct |
18 ms |
98376 KB |
Output is correct |
13 |
Correct |
35 ms |
99164 KB |
Output is correct |
14 |
Correct |
21 ms |
98396 KB |
Output is correct |
15 |
Correct |
23 ms |
98644 KB |
Output is correct |
16 |
Correct |
57 ms |
100308 KB |
Output is correct |
17 |
Correct |
55 ms |
100176 KB |
Output is correct |
18 |
Correct |
71 ms |
100956 KB |
Output is correct |
19 |
Correct |
23 ms |
98540 KB |
Output is correct |
20 |
Correct |
92 ms |
102236 KB |
Output is correct |
21 |
Correct |
39 ms |
107868 KB |
Output is correct |
22 |
Correct |
29 ms |
102416 KB |
Output is correct |
23 |
Correct |
94 ms |
130576 KB |
Output is correct |
24 |
Correct |
208 ms |
176564 KB |
Output is correct |
25 |
Correct |
81 ms |
121172 KB |
Output is correct |
26 |
Correct |
99 ms |
131672 KB |
Output is correct |
27 |
Correct |
151 ms |
160688 KB |
Output is correct |
28 |
Correct |
61 ms |
118608 KB |
Output is correct |
29 |
Correct |
100 ms |
138316 KB |
Output is correct |
30 |
Correct |
236 ms |
196124 KB |
Output is correct |
31 |
Correct |
225 ms |
195980 KB |
Output is correct |
32 |
Correct |
220 ms |
196480 KB |
Output is correct |
33 |
Correct |
201 ms |
196320 KB |
Output is correct |
34 |
Execution timed out |
2058 ms |
196184 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |