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;
const long long INF = 1e15 + 15;
void maximize(long long& x, long long y) {
x = max(x, y);
}
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<vector<array<int, 3>>>>> path(n, vector<vector<vector<array<int, 3>>>> (3, vector<vector<array<int, 3>>> (k + 1)));
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]);
}
}
}
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)));
function<void(int, int, int)> dfs = [&](int i, int st, int ck) {
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);
}
};
dfs(n - 1, 2, k);
cout << dp[n - 1][2][k] << '\n';
for (int i = 0; i < n; ++i) {
cout << used[i];
}
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... |