이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int m;
ll L, all = 0;
cin >> m >> L;
vector<ll> _a(2 * m + 1), _b(2 * m + 1);
auto a = _a.data() + m, b = _b.data() + m;
for (int i = -m; i <= m; i++) {
cin >> a[i], all += max(0, i) * a[i];
}
if (L < 0) {
L *= -1, all = 0;
for (int i = 1; i <= m; i++) {
swap(a[i], a[-i]), all += i * a[i];
}
}
if (all < L) cout << "impossible\n", exit(0);
ll ans = a[0];
for (int i = -1; i >= -m; i--) {
b[i] = min(a[i], (all - L) / -i);
ans += b[i], L -= b[i] * i, a[i] -= b[i];
}
for (int i = 1; i <= m; i++) {
b[i] = min(a[i], L / i);
ans += b[i], L -= b[i] * i, a[i] -= b[i];
}
int lim = m * L;
vector<int> _f(2 * lim + 1, -1e9);
auto f = _f.data() + lim;
f[0] = 0;
for (int i = -m; i <= m; i++) if (i) {
auto _g = _f;
auto g = _g.data() + lim;
auto tr = [&](int x) {
if (i * x > 0) {
for (int j = lim - i * x; j >= -lim; j--) {
if (g[j + i * x] < g[j] + x) g[j + i * x] = g[j] + x;
}
} else {
for (int j = -lim - i * x; j <= lim; j++) {
if (g[j + i * x] < g[j] + x) g[j + i * x] = g[j] + x;
}
}
};
a[i] = min(a[i], 2LL * lim / abs(i));
for (int x = 1; x <= a[i]; x <<= 1) tr(x), a[i] -= x;
if (a[i]) tr(a[i]);
swap(_f, _g), swap(f, g);
b[i] = min(b[i], 2LL * lim / abs(i));
for (int x = 1; x <= b[i]; x <<= 1) tr(-x), b[i] -= x;
if (b[i]) tr(-b[i]);
for (int j = -lim; j <= lim; j++) {
f[j] = max(f[j], g[j]);
}
}
if (f[L] < -1e8) cout << "impossible\n";
else cout << ans + f[L] << "\n";
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... |
# | 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... |