제출 #573795

#제출 시각아이디문제언어결과실행 시간메모리
573795eecsUplifting Excursion (BOI22_vault)C++17
100 / 100
1120 ms1712 KiB
#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 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...