This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int k, n, m, a, b, ans;
int val[1005], s[5005], t[5005];
int dp[2][5005][2][2];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>k>>n>>m>>a>>b;
for(int i=1; i<=k; i++) cin>>val[i];
for(int i=1; i<=n; i++) cin>>s[i];
for(int i=1; i<=m; i++){
cin>>t[i];
for(int a=0; a<2; a++) for(int b=0; b<2; b++)
dp[0][i][a][b]=-1000000000;
}
ans=a+m*b;
for(int i=1, id=1; i<=n; i++, id^=1){
for(int a=0; a<2; a++) for(int b=0; b<2; b++)
dp[id][0][a][b]=-1000000000;
for(int j=1; j<=m; j++){
if(s[i]==t[j]){
int tmp=(j==1)?0:(a+(j-1)*b);
for(int a=0; a<2; a++) for(int b=0; b<2; b++)
tmp=max(tmp, dp[id^1][j-1][a][b]);
dp[id][j][1][1]=tmp+val[t[j]];
}else dp[id][j][1][1]=-1000000000;
dp[id][j][1][0]=max(dp[id][j-1][1][0]+b, dp[id][j-1][1][1]+a+b);
dp[id][j][0][1]=max(dp[id^1][j][0][1]+b, dp[id^1][j][1][1]+a+b);
dp[id][j][0][0]=max(max(dp[id][j-1][0][0]+b, dp[id][j-1][0][1]+a+b), 2*a+b*(j+1));
}
ans=max(ans, a+m*b);
for(int a=0; a<2; a++) for(int b=0; b<2; b++)
ans=max(ans, dp[id][m][a][b]);
}
cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |