This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// BalticOI 2022 Day1 Uplifting Excursion
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
LL M, L;
cin >> M >> L;
vector<LL> A(2 * M + 1), C(2 * M + 1);
for (LL i = 0; i <= 2 * M; i++) cin >> A[i];
LL sum = 0; // -M ... 0, 先把负的都算上
for (LL i = 0; i <= M; i++) sum += A[i] * (i - M), C[i] = A[i];
for (LL i = M + 1; i <= 2 * M; i++) { // 再不超过L的前提下贪心选择正的
C[i] = min((L - sum) / (i - M), A[i]), sum += C[i] * (i - M);
assert(sum <= L);
}
for (LL i = 0; i < M; i++) {
assert(sum <= L);
LL c = min((L - sum) / (M - i), A[i]);
sum += c * (M - i), C[i] -= c;
}
if (L - sum > M) return puts("impossible"), 0;
LL cs = 0, wc = 0;
const LL MIF = -4e18, B = M * M;
vector<LL> W{0}, V{0}; // 二进制分组
for (LL i = 0; i <= 2 * M; i++) {
cs += C[i]; // 已有的数量
if (i == M) continue;
LL k = 1, s = min(A[i] - C[i], B), w = i - M; // 增加物品
while (k <= s) W.push_back(w * k), V.push_back(k), s -= k, k *= 2;
if (s > 0) W.push_back(w * s), V.push_back(s);
k = 1, s = min(C[i], B); // 消去物品
while (k <= s) W.push_back(-w * k), V.push_back(-k), s -= k, k *= 2;
if (s > 0) W.push_back(-w * s), V.push_back(-s);
}
vector<vector<LL>> F(2, vector<LL>(2 * B + 4, MIF));
F[0][B] = 0, wc = W.size() - 1;
for (LL i = 1; i <= wc; i++) {
int ci = i % 2, i1 = ci ^ 1;
for (LL j = 2 * B; j >= 0; j--) {
F[ci][j] = F[i1][j];
if (j >= W[i] and j - W[i] <= 2 * B and F[i1][j - W[i]] != MIF)
F[ci][j] = max(F[ci][j], F[i1][j - W[i]] + V[i]);
}
}
LL f = F[wc % 2][B + L - sum];
if (f + cs < 0)
puts("impossible");
else
printf("%lld\n", f + cs);
return 0;
}
// AC 100
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |