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;
struct Item{
int value, weight, quantity;
};
vector<int>weights, values;
vector<vector<long long> >table;
long long rec(int indx, int W){
if(W < 0)return -1e18;
if(indx == weights.size() || W == 0)return 0;
if(table[W][indx] != -1)return table[W][indx];
return table[W][indx] = max(rec(indx + 1, W - weights[indx]) + values[indx], rec(indx + 1, W));
}
int main()
{
int S, n;
cin >> S >> n;
vector<Item>items;
for(int i = 0;i < n;++i){
int v, w, q;
cin >> v >> w >> q;
q = min(q, S / w);
Item cur;
cur.value = v;
cur.weight = w;
cur.quantity = q;
items.push_back(cur);
}
for(int i = 0;i < n;++i){
int v = items[i].value, w = items[i].weight, q = items[i].quantity;
for(int j = 0;j < q;++j){
weights.push_back(w);
values.push_back(v);
}
}
table.assign(S + 1, vector<long long>(weights.size(), -1));
cout << rec(0, S);
}
Compilation message (stderr)
knapsack.cpp: In function 'long long int rec(int, int)':
knapsack.cpp:10:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | if(indx == weights.size() || W == 0)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... |