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>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 5e3 + 5, inf = 1e9;
int f[2][maxn][2][2];
int val[maxn], s[maxn], t[maxn], A, B, N, M, K;
int best(int i, int j)
{
  int res;
  if(j) res = A + B * j;
  else res = 0;
  for(int a = 0; a < 2; ++a)
    for(int b = 0; b < 2; ++b)
      res = max(res, f[i][j][a][b]);
  return res;
}
signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  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 j = 1; j <= M; ++j)
    for(int a = 0; a < 2; ++a)
      for(int b = 0; b < 2; ++b)
        f[0][j][a][b] = -inf;
  int res = A + M * B;
  for(int i = 1, z = 1; z <= N; ++z, i ^= 1){
    for(int a = 0; a < 2; ++a)
      for(int b = 0; b < 2; ++b)
        f[i][0][a][b] = -inf;
    for(int j = 1; j <= M; ++j){
      if(s[z] == t[j]) f[i][j][1][1] = best(i, j - 1) + val[s[z]];
      else f[i][j][1][1] = -inf;
      f[i][j][1][0] = max(f[i][j - 1][1][0] + B, f[i][j - 1][1][1] + A + B);
      f[i][j][0][1] = max(f[i ^ 1][j][0][1] + B, f[i ^ 1][j][1][1] + A + B);
      f[i][j][0][0] = max(f[i][j - 1][0][0] + B, f[i][j - 1][0][1] + A + B);
      f[i][j][0][0] = max(f[i][j][0][0], 2 * A + B * (j + 1));
    }
    res = max(res, best(i, M));
  }
  cout << res;
}
| # | 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... |