QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#492175 | #7279. Tricks of the Trade | shiomusubi496 | 0 | 0ms | 3600kb | C++17 | 2.1kb | 2024-07-26 10:10:22 | 2024-07-26 10:10:22 |
answer
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
using namespace std;
using ll = long long;
constexpr ll inf = 1e18;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
int main() {
int N, K; cin >> N >> K;
vector<ll> C(N), V(N);
rep (i, N) cin >> C[i];
rep (i, N) cin >> V[i];
if (K == 1) {
ll ans = -inf;
rep (i, N) chmax(ans, V[i] - C[i]);
cout << ans << endl;
rep (i, N) cout << (V[i] - C[i] == ans);
cout << endl;
return 0;
}
vector<ll> CC(N + 1);
rep (i, N) CC[i + 1] = CC[i] + C[i];
vector dp(N + 1, vector<ll>(K + 1, -inf));
dp[0][0] = 0;
rep (i, N) rep (j, K + 1) {
chmax(dp[i + 1][j], dp[i][j]);
if (j == 0) chmax(dp[i + 1][j + 1], dp[i][j] + CC[i] + V[i]);
else if (j == K - 1) chmax(dp[i + 1][j + 1], dp[i][j] + V[i] - CC[i + 1]);
else chmax(dp[i + 1][j + 1], dp[i][j] + V[i]);
}
cout << dp[N][K] << endl;
vector<bool> ans(N);
vector<vector<bool>> used(N + 1, vector<bool>(K + 1, false));
used[N][K] = true;
rrep (i, N) rep (j, K + 1) {
if (used[i + 1][j] && dp[i + 1][j] == dp[i][j]) used[i][j] = true;
if (used[i + 1][j + 1]) {
if (j == 0) {
if (dp[i + 1][j + 1] == dp[i][j] + CC[i] + V[i]) used[i][j] = true, ans[i] = true;
}
else if (j == K - 1) {
if (dp[i + 1][j + 1] == dp[i][j] + V[i] - CC[i + 1]) used[i][j] = true, ans[i] = true;
}
else {
if (dp[i + 1][j + 1] == dp[i][j] + V[i]) used[i][j] = true, ans[i] = true;
}
}
}
rep (i, N) cout << ans[i];
cout << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 10
Accepted
time: 0ms
memory: 3600kb
input:
5 3 3 5 2 3 6 2 1 5 2 3
output:
-1 00111
result:
ok all correct
Test #2:
score: 0
Runtime Error
input:
200 40 81 50 87 91 54 1 77 68 19 84 28 81 45 64 4 13 25 89 12808135 86 40 73 47 18 82 79 11 96 16 34 80 36 8 5 60 76 86 84 36 37 96 55 68 12807879 51 89 57 30 100 11 44 19 96 32 9 68 41 12808009 5 58 12807939 92 68 67 78 32 57 49 71 92 64 35 89 76 81 36 6 6 53 31 85 79 5 85 1 91 17 37 25 91 54 29 47...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Runtime Error
Test #30:
score: 10
Accepted
time: 0ms
memory: 3508kb
input:
5 2 1 6 1 5 2 4 1 6 2 4
output:
2 10111
result:
ok all correct
Test #31:
score: 0
Runtime Error
input:
250000 2 18 35 29 35 18 610084694 18 35 29 35 18 448867144 18 35 29 35 18 971272498 18 35 29 35 18 890430190 18 35 29 35 18 655685684 18 35 29 35 18 234608237 18 35 29 35 18 894586749 18 35 29 35 18 442195168 18 35 29 35 18 341564617 18 35 29 35 18 985069087 18 35 29 35 18 967546483 18 35 29 35 18 5...
output:
33391
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%