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 ll long long
#define f first
#define s second
#define pb push_back
#define inf 1000000000000
using namespace std;
ll k,n,m,A,B,v[5005],s[5005],t[5005];
ll dp[5005][5005],mx[5005];
int main(){
ios::sync_with_stdio(false);
cin >> k >> n >> m >> A >> B;
A = -A;
B = -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 i=0; i<=n; i++)
for(int j=0; j<=m; j++)
dp[i][j] = -inf;
for(int i=0; i<=m; i++)
mx[i] = -inf;
for(int i=1; i<=m; i++){
if(s[1] == t[i])
if(i == 1)dp[1][i] = v[s[1]];
else dp[1][i] = v[s[1]] - A - (i - 1) * B;
}
for(int i=2; i<=n; i++){
ll cur = -inf;
for(int j=1; j<=m; j++){
dp[i][j] = -inf;
if(s[i] != t[j]){
cur = max(cur - B , max(mx[j - 1] , dp[i - 1][j - 1]) - A - B);
continue;
}
if(j == 1){
dp[i][j] = v[t[1]];
}
else {
dp[i][j] = v[s[i]] - A - (j - 1) * B;
}
dp[i][j] = max(dp[i][j] , max(dp[i - 1][j - 1] , cur) + v[s[i]]);
dp[i][j] = max(dp[i][j] , mx[j - 1] + v[s[i]]);
cur = max(cur - B , max(mx[j - 1] , dp[i - 1][j - 1]) - A - B);
}
for(int j=1; j<=m; j++){
mx[j] = max(mx[j] - B , dp[i - 1][j] - A - B);
}
}
ll ans = -A - m * B;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++){
if(j == m)
ans = max(ans , dp[i][j]);
else ans = max(ans , dp[i][j] - A - (m - j) * B);
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
VisitingSingapore.cpp: In function 'int main()':
VisitingSingapore.cpp:28:11: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
28 | if(s[1] == t[i])
| ^
# | 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... |