Submission #693005

#TimeUsernameProblemLanguageResultExecution timeMemory
693005saayan007Visiting Singapore (NOI20_visitingsingapore)C++17
100 / 100
654 ms848 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define fur(i, a, b) for(ll i = a; i <= (ll) b; ++i) #define ruf(i, a, b) for(ll i = a; i >= (ll) b; --i) #define fr first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define nl "\n" const ll inf = 1e10L; ll k, n, m, a, b; ll cost(ll y, ll x) { ll l, r; ll ans = 0; l = y%2, r = x%2; if(r == 0) { ans -= b; if(l == 1) ans -= a; } y /= 2; x /= 2; l = y%2, r = x%2; if(r == 0) { ans -= b; if(l == 1) ans -= a; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> k >> n >> m >> a >> b; a = -a; b = -b; ll v[k + 1], s[n + 1], t[m + 1]; fur(i, 1, k) { cin >> v[i]; } fur(i, 1, n) { cin >> s[i]; } fur(i, 1, m) { cin >> t[i]; } ll dp[2][m + 1][4]; fur(i, 0, 1) fur(j, 0, m) fur(x, 0, 3) dp[i][j][x] = -inf; ll res = -a -m*b; fur(i, 1, n) { ll cur = i % 2; ll prv = 1 - cur; fur(j, 1, m) { if(s[i] == t[j]) { fur(y, 0, 3) { dp[cur][j][3] = max({dp[cur][j][3], dp[prv][j - 1][y] + v[t[j]] + cost(y, 3), v[t[j]] + (j == 1 ? 0 : - a - (j - 1) * b)}); res = max(res, dp[cur][j][3] + (j == m ? 0 : - a - b * (m - j))); } } dp[cur][j][0] = max({dp[cur][j][0], dp[prv][j][0] + cost(0, 0), dp[prv][j][2] + cost(2, 0), dp[cur][j - 1][0] + cost(0, 0), dp[cur][j - 1][1] + cost(1, 0)}); fur(y, 0, 3) { dp[cur][j][0] = max(dp[cur][j][0], dp[prv][j - 1][y] + cost(y, 0)); } dp[cur][j][1] = max({dp[cur][j][1], dp[prv][j][1] + cost(1, 1), dp[prv][j][3] + cost(3, 1)}); dp[cur][j][2] = max({dp[cur][j][2], dp[cur][j - 1][2] + cost(2, 2), dp[cur][j - 1][3] + cost(3, 2)}); } fur(j, 0, m) { fur(x, 0, 3) { dp[prv][j][x] = -inf; } } } cout << res << nl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...