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;
#define ll long long
int const M = 350;
int const MM = M*M;
int const off = 340;
int const off2 = off*off;
ll A[2*M];
ll dp[2][2*MM];
int m; ll l;
int main() {
cin >> m >> l;
for (int i = -m; i <= m; ++i) cin >> A[i+off];
ll zz = 0;
for (int i = -m; i <= m; ++i) {
zz += A[i+off] * i;
}
if (zz < 0) {
l = -l;
for (int i = -m; i < 0; ++i) {
int j = -i;
int idx = i+off, idx2 = j+off;
swap(A[idx], A[idx2]);
}
}
ll itm = 0;
memset(dp, 0xc3, sizeof dp);
dp[0][off2] = 0;
int idp = 1;
for (int i = -m; i <= m; ++i) {
ll take = i <= 0 ? A[i+off] : min(A[i+off], max(0LL, l / i));
l -= take * i;
A[i+off] -= take;
itm += take;
for (int j = -m*m; j <= m*m; ++j) dp[idp][j+off2] = dp[idp^1][j+off2];
if (i != 0) {
// try_add_to_dp A[i+off]
for (int j = -m*m; j <= m*m; ++j) {
for (int k = 1; (k <= A[i+off]) && (j-k*i>=-m*m) && (j-k*i<=m*m); ++k) {
dp[idp][j+off2] = max(dp[idp][j+off2], dp[idp^1][j-k*i+off2] + k);
}
}
// try_delete_from_dp take
for (int j = -m*m; j <= m*m; ++j) {
for (int k = 1; (k <= take) && (j+k*i>=-m*m) && (j+k*i<=m*m); ++k) {
dp[idp][j+off2] = max(dp[idp][j+off2], dp[idp^1][j+k*i+off2] - k);
}
}
}
idp ^= 1;
}
idp ^= 1;
if (l < -m*m || l > m*m || dp[idp][l+off2] < -1e9) {
cout << "impossible\n";
}
else {
cout << (dp[idp][l+off2] + itm) << '\n';
}
}
# | 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... |