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 <iostream>
using namespace std;
/*
ifstream cin("txt.in");
ofstream cout("txt.out");
*/
  
const int INF = -1e9;
const int KMAX = 1000;
const int NMAX = 5000;
int K, N, M, A, B;
int dp[2][NMAX + 5][2][2];
int v[KMAX + 5], s[NMAX + 5], t[NMAX + 5];
void Upd(int i, int j, int x, int y)
{
    if(i < N && j < M && s[i + 1] == t[j + 1])
    {
        dp[!(i & 1)][j + 1][0][0] = max(dp[!(i & 1)][j + 1][0][0], dp[i & 1][j][x][y] + v[s[i + 1]]);
    }
    if(i < N)
    {
        dp[!(i & 1)][j][1][y] = max(dp[!(i & 1)][j][1][y], dp[i & 1][j][x][y] + B + ((x == 0) ? A : 0));
    }
    if(j < M)
    {
        dp[i & 1][j + 1][x][1] = max(dp[i & 1][j + 1][x][1], dp[i & 1][j][x][y] + B + ((y == 0) ? A : 0));
    }
}
int main()
{
    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];
    for(int j = 0; j <= M; j++)
        for(int x = 0; x < 2; x++)
            for(int y = 0; y < 2; y++)
                dp[0][j][x][y] = INF;
    dp[0][0][0][0] = 0;
    int res = A + M * B;
    for(int i = 0; i <= N; i++)
    {
        for(int j = 0; j <= M; j++)
            for(int x = 0; x < 2; x++)
                for(int y = 0; y < 2; y++)
                    dp[!(i & 1)][j][x][y] = INF;
        dp[!(i & 1)][0][0][0] = 0;
        for(int j = 0; j <= M; j++)
            for(int x = 0; x < 2; x++)
                for(int y = 0; y < 2; y++)
                    if(dp[i & 1][j][x][y] != INF)
                        Upd(i, j, x, y);
        for(int x = 0; x < 2; x++)
            for(int y = 0; y < 2; y++)
                res = max(res, dp[i & 1][M][x][y]);
    }
    cout << res << '\n';
    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... |