#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=5005;
const ll inf=1e18;
long long mn2[5005][5005],mn1[5005][5005];
long long k,n,m,a,b,s[maxN],v[maxN],t[maxN];
long long dp[5005][5005];
void update(ll i,ll j,long long val)
{
mn1[i][j]=max(mn1[i][j-1],val);
mn2[i][j]=max(mn2[i-1][j],val);
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
dp[i][j]=max(dp[i][j],1ll*val);
}
void solve()
{
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];
dp[0][0]=mn1[0][0]=mn2[0][0]=-inf;
for(int i=1;i<=n;i++)
{
dp[i][0]=dp[0][i]=mn1[i][0]=mn1[0][i]=mn2[0][i]=mn2[i][0]=-inf;
}
long long ans=-inf;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s[i]==t[j])
{
if(i<=1||j<=1) dp[i][j]=-inf;
dp[i][j]=max(dp[i][j],1ll*v[s[i]]+(a*(j>1)+b*(j-1)));
ll x;
x=mn1[i-1][j-1];
dp[i][j]=max(dp[i][j],1ll*x+i*b+j*b-2*b+a+v[s[i]]);
x=mn2[i-1][j-1];
dp[i][j]=max(dp[i][j],1ll*x+i*b+j*b-2*b+a+v[s[i]]);
if(i>=2&&j>=2)
{
x=dp[i-2][j-2];
dp[i][j]=max(dp[i][j],1ll*x+i*b+j*b-2*b+2*a+v[s[i]]);
}
}
else
{
dp[i][j]=-inf;
}
dp[i+1][j+1]=dp[i][j]+v[s[i+1]];
ans=max(ans,1ll*dp[i][j]+(a*(m>j)+b*(m-j)));
update(i,j,dp[i][j]-b*i-b*j);
}
}
cout << max(ans,a+b*m);
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
24 ms |
31752 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
5 ms |
11220 KB |
Output is correct |
5 |
Correct |
2 ms |
4820 KB |
Output is correct |
6 |
Correct |
11 ms |
15228 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
15 ms |
20980 KB |
Output is correct |
10 |
Correct |
27 ms |
36016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
7 ms |
9388 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
3 ms |
3796 KB |
Output is correct |
6 |
Correct |
14 ms |
18756 KB |
Output is correct |
7 |
Correct |
15 ms |
17748 KB |
Output is correct |
8 |
Correct |
19 ms |
26392 KB |
Output is correct |
9 |
Correct |
3 ms |
4692 KB |
Output is correct |
10 |
Correct |
30 ms |
35980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
89292 KB |
Output is correct |
2 |
Correct |
27 ms |
53844 KB |
Output is correct |
3 |
Correct |
145 ms |
239176 KB |
Output is correct |
4 |
Runtime error |
151 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
89292 KB |
Output is correct |
2 |
Correct |
27 ms |
53844 KB |
Output is correct |
3 |
Correct |
145 ms |
239176 KB |
Output is correct |
4 |
Runtime error |
151 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
89292 KB |
Output is correct |
2 |
Correct |
27 ms |
53844 KB |
Output is correct |
3 |
Correct |
145 ms |
239176 KB |
Output is correct |
4 |
Runtime error |
151 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
1 ms |
1748 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
1 ms |
1732 KB |
Output is correct |
15 |
Correct |
1 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
24 ms |
31752 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
5 ms |
11220 KB |
Output is correct |
5 |
Correct |
2 ms |
4820 KB |
Output is correct |
6 |
Correct |
11 ms |
15228 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
15 ms |
20980 KB |
Output is correct |
10 |
Correct |
27 ms |
36016 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
7 ms |
9388 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
3 ms |
3796 KB |
Output is correct |
16 |
Correct |
14 ms |
18756 KB |
Output is correct |
17 |
Correct |
15 ms |
17748 KB |
Output is correct |
18 |
Correct |
19 ms |
26392 KB |
Output is correct |
19 |
Correct |
3 ms |
4692 KB |
Output is correct |
20 |
Correct |
30 ms |
35980 KB |
Output is correct |
21 |
Correct |
51 ms |
89292 KB |
Output is correct |
22 |
Correct |
27 ms |
53844 KB |
Output is correct |
23 |
Correct |
145 ms |
239176 KB |
Output is correct |
24 |
Runtime error |
151 ms |
262144 KB |
Execution killed with signal 9 |
25 |
Halted |
0 ms |
0 KB |
- |