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;
typedef pair<int, int> pii;
typedef long long ll;
const int MAXN = 100;
const int MXVAL = 505000;
int n;
ll target;
int cnt[2*MAXN+1];
int mx_use[MXVAL+1];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> target;
if (abs(target) > MXVAL) {
cout << "impossible\n";
return 0;
}
fill(mx_use, mx_use+MXVAL+1, -1);
mx_use[0] = 0;
for (int i = 0; i <= 2*n; i++) cin >> cnt[i];
if (target < 0) {
target *= -1;
for (int i = 0; i < n; i++) swap(cnt[i], cnt[2*n-i]);
}
for (int j = 1; j <= n; j++) {
for (int i = MXVAL; i >= j; i--) {
for (int p = 1; (p <= cnt[n+j]) && (i-p*j >= 0); p++)
if (mx_use[i-p*j] >= 0) mx_use[i] = max(mx_use[i], mx_use[i-p*j]+p);
}
}
for (int j = 1; j <= n; j++) {
for (int i = target; i <= MXVAL-j; i++) {
for (int p = 1; (p <= cnt[n-j]) && (i+p*j <= MXVAL); p++)
if (mx_use[i+p*j] >= 0) mx_use[i] = max(mx_use[i], mx_use[i+p*j]+p);
}
}
if (mx_use[target] == -1) {
cout << "impossible\n";
return 0;
}
cout << (mx_use[target]+cnt[n]) << "\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... |