Submission #1071927

#TimeUsernameProblemLanguageResultExecution timeMemory
1071927NeroZeinTricks of the Trade (CEOI23_trade)C++17
20 / 100
1010 ms2097152 KiB
#include <bits/stdc++.h> using namespace std; const long long INF = 1e15 + 15; void maximize(long long& x, long long y) { x = max(x, y); } inline void dfs(int i, int st, int ck, vector<bool>& used, vector<vector<vector<bool>>>& vis, vector<vector<vector<vector<array<int, 3>>>>>& path) { if (vis[i][st][ck] == true) { return; } vis[i][st][ck] = true; if (i == 0 && ck == 1) { used[0] = true; } for (auto [pi, pst, pk] : path[i][st][ck]) { if (pk == ck - 1) { used[i] = true; } dfs(pi, pst, pk, used, vis, path); } } int main() { int n, k; cin >> n >> k; vector<int> c(n), s(n); for (int i = 0; i < n; ++i) { cin >> c[i]; } for (int i = 0; i < n; ++i) { cin >> s[i]; } vector<vector<vector<long long>>> dp(n, vector<vector<long long>> (3, vector<long long> (k + 1, -INF))); dp[0][0][0] = 0; dp[0][1][1] = s[0] - c[0]; if (k == 1) { dp[0][2][1] = s[0] - c[0]; } for (int i = 1; i < n; ++i) { for (int st = 0; st < 3; ++st) { if (st == 0) { maximize(dp[i][0][0], dp[i - 1][0][0]); maximize(dp[i][1][1], dp[i - 1][0][0] + s[i] - c[i]); } else if (st == 1) { for (int j = 1; j < k; ++j) { maximize(dp[i][1][j], dp[i - 1][1][j] - c[i]); maximize(dp[i][1][j], dp[i - 1][1][j - 1] + s[i] - c[i]); } maximize(dp[i][2][k], dp[i - 1][1][k - 1] + s[i] - c[i]); } else { maximize(dp[i][2][k], dp[i - 1][2][k]); } } } vector<vector<vector<vector<array<int, 3>>>>> path(n, vector<vector<vector<array<int, 3>>>> (3, vector<vector<array<int, 3>>> (k + 1))); for (int i = 1; i < n; ++i) { for (int st = 0; st < 3; ++st) { if (st == 0) { if (dp[i][0][0] == dp[i - 1][0][0]) { path[i][0][0].push_back({i - 1, 0, 0}); } if (dp[i][1][1] == dp[i - 1][0][0] + s[i] - c[i]) { path[i][1][1].push_back({i - 1, 0, 0}); } } else if (st == 1) { for (int j = 1; j < k; ++j) { if (dp[i][1][j] == dp[i - 1][1][j] - c[i]) { path[i][1][j].push_back({i - 1, 1, j}); } if (dp[i][1][j] == dp[i - 1][1][j - 1] + s[i] - c[i]) { path[i][1][j].push_back({i - 1, 1, j - 1}); } } if (dp[i][2][k] == dp[i - 1][1][k - 1] + s[i] - c[i]) { path[i][2][k].push_back({i - 1, 1, k - 1}); } } else { if (dp[i][2][k] == dp[i - 1][2][k]) { path[i][2][k].push_back({i - 1, 2, k}); } } } } vector<bool> used(n); vector<vector<vector<bool>>> vis(n, vector<vector<bool>> (3, vector<bool> (k + 1))); dfs(n - 1, 2, k, used, vis, path); cout << dp[n - 1][2][k] << '\n'; for (int i = 0; i < n; ++i) { cout << used[i]; } return 0; }
#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...