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;
int unboundedKnapsackWithLimits(vector<int>& weights, vector<int>& values, vector<int>& limits, int W) {
int n = weights.size();
// vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
// for (int i = 1; i <= n; ++i) {
// for (int w = 1; w <= W; ++w) {
// dp[i][w] = dp[i - 1][w];
// for (int j = 1; j <= min(limits[i - 1], w / weights[i - 1]); ++j) {
// dp[i][w] = max(dp[i][w], dp[i - 1][w - j * weights[i - 1]] + j * values[i - 1]);
// }
// }
// }
// return dp[n][W];
vector<vector<int>> dp(2, vector<int>(W + 1, 0));
for (int i = 1; i <= n; ++i) {
for (int w = 1; w <= W; ++w) {
dp[i%2][w] = dp[(i - 1)%2][w];
for (int j = 1; j <= min(limits[i - 1], w / weights[i - 1]); ++j) {
dp[i%2][w] = max(dp[i%2][w], dp[(i - 1)%2][w - j * weights[i - 1]] + j * values[i - 1]);
}
}
}
return dp[n%2][W];
}
int main() {
int z1, z2, z3, W, n;
cin >> W >> n;
vector<int> values, weights, limits;
while (n--) {
cin >> z1 >> z2 >> z3;
values.push_back(z1);
weights.push_back(z2);
limits.push_back(z3);
}
cout << unboundedKnapsackWithLimits(weights, values, limits, W) << 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... |