#include <bits/stdc++.h>
#include <chrono>
#define ll long long int
#define endl '\n'
#define vn vector<ll>
#define vi vector<pair <ll,ll>>
using namespace std;
using namespace std::chrono;
const int MAX_N = 1e9 + 7;
#define pii pair<ll,ll>
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define pb push_back  
#define srt(vp) sort(vp.begin(), vp.end()) 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    auto start = high_resolution_clock::now();
    ll k,n,m,a,b;
    cin>>k>>n>>m>>a>>b;
    vn v(k+1);
    for (int i=1;i<=k;i++) cin>>v[i];
    vn s(n+1);
    for (int i=1;i<=n;i++) cin>>s[i];
    vn t(m+1);
    for (int i=1;i<=m;i++) cin>>t[i];
    const ll NEG=-4e18;
    vector<vector<array<ll,4>>> dp(2,vector<array<ll,4>>(m+1,{NEG,NEG,NEG,NEG}));
    ll ans=a+m*b;
    for(int i=1;i<=n;i++){
        int cur=i&1,pre=cur^1;
        for(int j=0;j<=m;j++) dp[cur][j]={NEG,NEG,NEG,NEG};
        for(int j=1;j<=m;j++){
            dp[cur][j][0]=max(dp[cur][j-1][0]-b,dp[pre][j][0]-b);
            dp[cur][j][1]=dp[pre][j][1]-b;
            dp[cur][j][2]=dp[cur][j-1][2]-b;
            if(s[i]==t[j]){
                ll best;
                if(j==1) best=v[t[j]];
                else best=v[t[j]]-a-(j-1)*b;
                best=max(best,dp[pre][j-1][0]-2*a+v[t[j]]);
                best=max(best,dp[pre][j-1][1]-a+v[t[j]]);
                best=max(best,dp[pre][j-1][2]-a+v[t[j]]);
                best=max(best,dp[pre][j-1][3]+v[t[j]]);
                dp[cur][j][3]=max(dp[cur][j][3],best);
                dp[cur][j][0]=max(dp[cur][j][0],best);
                dp[cur][j][1]=max(dp[cur][j][1],best);
                dp[cur][j][2]=max(dp[cur][j][2],best);
                if(j<m) ans=max(ans,best-a-(m-j)*b);
                else ans=max(ans,best);
            }
        }
    }
    cout<<ans<<endl;
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    //cout << "Time taken by function: " << duration.count() << " microseconds" << 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... |