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;
long long k,n,m,a,b,dp[5010][3][3],tab[5010][3][3],ans,arr[1010],s[5010],t[5010];
int main(){
cin>>k>>n>>m>>a>>b;
for(long long i =1;i<=k;i++){
cin>>arr[i];
}
for(long long i =1;i<=n;i++){
cin>>s[i];
}
for(long long i =1;i<=m;i++){
cin>>t[i];
}
ans = -1e9;
for(int j =0;j<=m;j++){
tab[j][0][1]=-1e9;
tab[j][0][0]=-1e9;
tab[j][1][0]=-1e9;
tab[j][1][1]=-1e9;
}
for(int i =1;i<=n;i++){
for(int j =1;j<=m;j++){
dp[j][0][1]=-1e9;
dp[j][0][0]=-1e9;
dp[j][1][0]=-1e9;
dp[j][1][1]=-1e9;
}
tab[0][0][0]=0;
for(int j =1;j<=m;j++){
if(s[i]==t[j]){
// if(j==1)dp[j][0][0]=arr[t[j]];
dp[j][0][0]=max(max(tab[j-1][0][0],tab[j-1][1][0]),max(tab[j-1][0][1],tab[j-1][1][1]))+arr[s[i]];
// dp[j][0][0] = max(dp[j][0][0],max(max(dp[j-1][0][0],dp[j-1][1][0]),max(dp[j-1][0][1],dp[j-1][1][1]))+arr[s[i]]);
}
// if(j==1){
// dp[j]
// continue;
// }
dp[j][1][0]= max(a+b+tab[j][0][0],b+tab[j][1][0]);
dp[j][0][1]= max(a+b+dp[j-1][0][0],b+dp [j-1][0][1]);
dp[j][1][1]= max(max(tab[j][0][1]+a+b,dp[j-1][1][0]+a+b),max(tab[j][1][1]+b,dp[j-1][1][1]+b));
}
// cout<<dp[m][0][0]<<endl;
ans = max(ans,max(max(dp[m][0][0],dp[m][1][0]),max(dp[m][0][1],dp[m][1][1])));
for(int j=0;j<=m;j++){
tab[j][0][0]=dp[j][0][0];
tab[j][1][0]=dp[j][1][0];
tab[j][0][1]=dp[j][0][1];
tab[j][1][1]=dp[j][1][1];
}
}
cout<<ans<<endl;
}
# | 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... |