This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/NOI18_knapsack > p83 e p199
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("03,unroll-loops")
const int MAXS = 2000;
const int MAXLOG = 15;
int dp[2][MAXS+1];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int S, n; cin >> S >> n;
vector<int> val(n);
vector<int> w(n);
vector<int> K(n);
vector<pair<int,int>> v; // peso, valor
v.reserve(n);
int tmpn=n;
for (int i=0; i<n; i++) {
cin >> val[i] >> w[i] >> K[i];
K[i] = min(K[i], (S/w[i]));
bool fi = true;
int other = K[i];
for (int b=MAXLOG; b>=0; b--) {
if (fi and ((1 << b) & K[i]) != 0) {
fi = false;
other -= ((1 << b) - 1);
} else if (!fi) {
tmpn++;
v.push_back({(1 << b) * w[i], (1 << b) * val[i]});
}
}
v.push_back({other * w[i], other * val[i]});
}
for (int i=1; i<=tmpn; i++) {
for (int P=0; P<=S; P++) {
if (P>=v[i-1].first) {
dp[i&1][P] = max(
dp[i&1][P],
dp[(i&1)^1][P-v[i-1].first] + v[i-1].second
);
}
}
}
cout << dp[tmpn&1][S] << endl;
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... |