# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
209815 | Alexa2001 | Visiting Singapore (NOI20_visitingsingapore) | C++17 | 0 ms | 0 KiB |
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;
const int Nmax = 5005, inf = 1e9;
int dp[2][Nmax][2][2];
int V[Nmax], S[Nmax], T[Nmax], A, B, n, m, k;
int best(int i, int j)
{
int r = dp[i][j][0][0];
r = max(r, dp[i][j][1][0]);
r = max(r, dp[i][j][0][1]);
r = max(r, dp[i][j][1][1]);
if(j) r = max(r, -A-B*j);
else r = max(r, 0);
return r;
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> k >> n >> m >> A >> B;
A = -A, B = -B;
int i, j, a, b;
for(i=1; i<=k; ++i) cin >> V[i];
for(i=1; i<=n; ++i) cin >> S[i];
for(i=1; i<=m; ++i) cin >> T[i];
for(j=1; j<=m; ++j)
{
for(a=0; a<2; ++a)
for(b=0; b<2; ++b)
dp[0][j][a][b] = -inf;
}
int ans = -A-m*B;
for(i=1, r=1; r<=n; ++r, i^=1)
{
for(a=0; a<2; ++a)
for(b=0; b<2; ++b)
dp[i][0][a][b] = -inf;
for(j=1; j<=m; ++j)
for(a=0; a<2; ++a)
for(b=0; b<2; ++b)
{
if(a == 1 && b == 1)
{
if(S[i] == T[j])
dp[i][j][a][b] = best(i^1, j-1) + V[S[i]];
else dp[i][j][a][b] = -inf;
continue;
}
if(a == 1 && b == 0)
{
dp[i][j][a][b] = max(dp[i][j-1][1][0] - B, dp[i][j-1][1][1] - A - B);
continue;
}
if(a == 0 && b == 1)
{
dp[i][j][a][b] = max(dp[i^1][j][0][1] - B, dp[i^1][j][1][1] - A - B);
continue;
}
if(a == 0 && b == 0)
{
dp[i][j][a][b] = max(dp[i][j-1][0][0] - B, dp[i][j-1][0][1] - A - B);
dp[i][j][a][b] = max(dp[i][j][a][b], -2*A-B*(j+1));
continue;
}
}
ans = max(ans, best(i, m));
}
cout << ans << '\n';
return 0;
}