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;
using ll = long long;
using ld = long double;
const int MOD = 998244353;
ll binpow(ll a, ll p, int mod = MOD) {
ll res = 1;
while (p) {
if (p & 1) {
(res *= a) %= mod;
}
p >>= 1;
(a *= a) %= mod;
}
return res;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
const int INF = -1e9;
using state = array<array<int, 2>, 2>;
const state INIT = state{array<int, 2>{INF, INF}, array<int, 2>{INF, INF}};
void solve() {
int K, n, m;
int A, B;
cin >> K >> n >> m >> A >> B;
vector<int> v(K + 1);
for (int i = 1; i <= K; i++) {
cin >> v[i];
}
vector<int> s(n + 1);
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
vector<int> t(n + 1);
for (int i = 1; i <= m; i++) {
cin >> t[i];
}
auto chmax = [&](int& cur, int val) {
if (val < INF / 2) return;
cur = max(cur, val);
};
int ans = A + B * m;
vector<state> dp(m + 1, INIT);
for (int i = 1; i <= n; i++) {
vector<state> nxt(m + 1, INIT);
for (int j = 0; j <= m; j++) {
if (j && s[i] == t[j]) {
chmax(nxt[j][1][1], v[s[i]] + A * (j > 1) + B * (j - 1));
}
for (int f = 0; f < 2; f++) {
for (int g = 0; g < 2; g++) {
chmax(nxt[j][0][g], dp[j][f][g] + A * f + B);
if (j) chmax(nxt[j][f][0], nxt[j - 1][f][g] + A * g + B);
if (j && s[i] == t[j]) {
chmax(nxt[j][1][1], dp[j - 1][f][g] + v[s[i]]);
}
}
}
for (int f = 0; f < 2; f++) {
for (int g = 0; g < 2; g++) {
chmax(ans, nxt[j][f][g] + A * (m - j > 0) + B * (m - j));
}
}
}
dp = move(nxt);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
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... |