This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;
#define For(i, a, b) for(int i = (a); i < (b); ++i)
#define ForE(i, a, b) for(int i = (a); i <= (b); ++i)
int K, n, m, A, B;
int hpn[1005], events[5005], mission[5005];
int dp[5005][5005][2][2], nxt[5005][1005], lst[1005];
int cal(int val) {
if (val == 0) return 0;
return A + val * B;
}
void chkmax(int &val, int nval) {
val = (val < nval ? nval : val);
}
void solve() {
cin >> K >> n >> m >> A >> B;
for (int i = 1; i <= K; ++i) cin >> hpn[i];
for (int i = 1; i <= n; ++i) cin >> events[i];
for (int i = 1; i <= m; ++i) cin >> mission[i];
memset(nxt[m + 1], -1, sizeof nxt[0]);
for (int i = m; i >= 1; --i) {
for (int j = 1; j <= K; ++j) nxt[i][j] = nxt[i + 1][j];
nxt[i][mission[i]] = i;
}
auto get_new_day = [&]() -> void {
ForE(i, 0, m) For(cont, 0, 2) For(gotA, 0, 2) {
dp[1][i][cont][gotA] = INT_MIN;
}
dp[1][0][1][1] = 0;
};
int res = INT_MIN;
auto getmax = [&]() -> void {
ForE(i, 0, m) For(cont, 0, 2) For(gotA, 0, 2) if (dp[0][i][cont][gotA] != INT_MIN) {
chkmax(res, dp[0][i][cont][gotA] + cal(m - i));
}
};
ForE(i, 0, m) For(cont, 0, 2) For(gotA, 0, 2) {
dp[0][i][cont][gotA] = INT_MIN;
}
dp[0][0][1][0] = 0;
getmax();
For(day, 0, n) {
get_new_day();
ForE(i, 0, m) For(cont, 0, 2) For(gotA, 0, 2) if (dp[0][i][cont][gotA] != INT_MIN) {
int val = dp[0][i][cont][gotA];
if (i && nxt[i + 1][mission[i]] != -1) {
int ni = nxt[i + 1][mission[i]];
chkmax(dp[0][ni][1][1], val + (ni - i) * B + (gotA ? 0 : A));
}
if (nxt[i + 1][events[day + 1]] != -1) {
int ni = nxt[i + 1][events[day + 1]];
chkmax(dp[1][ni][1][(i + 1 != ni)], val + hpn[events[day + 1]] + cal(ni - i - 1));
}
chkmax(dp[1][i][0][gotA], val + (cont ? A + B : B));
}
memcpy(dp[0], dp[1], sizeof dp[0]);
getmax();
}
// int res = INT_MIN;
// ForE(day, 0, n) ForE(i, 0, m) For(cont, 0, 2) if (dp[day][i][cont] != INT_MIN) {
// chkmax(res, dp[day][i][cont] + cal(m - i));
// }
cout << res << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
// cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
// cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
// cout << "Check array size pls sir" << endl;
#endif
}
# | 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... |