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 main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
// freopen("Knapsack.in", "r", stdin);
// freopen("Knapsack.out", "w", stdout);
int limit, type_num;
cin >> limit >> type_num;
vector<vector<int>> items(type_num, vector<int>(3));
for (int i = 0; i < type_num; i++) {
int weight, num, val;
cin >> val >> weight >> num;
if (weight>limit) continue;
items[i][0] = weight; items[i][1] = num; items[i][2] = val;
}
vector<int> dp(limit+1, -1);
dp[0]= 0;
int dylimit = 0;
for (int t=0; t<items.size(); t++) {
int weight, num, val;
weight = items[t][0]; num = items[t][1]; val = items[t][2];
for (int i=min(dylimit - weight, 0); i>=0; i--) {
if (dp[i]>=0) {
for (int k=1; k <= num && i+weight*k <= limit; k++) {
dp[i+weight*k] = max(dp[i+weight*k], dp[i] + k*val);
dylimit = i+weight*k;
}
}
}
}
cout << *max_element(dp.begin(), dp.end()) << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:28:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int t=0; t<items.size(); t++) {
| ~^~~~~~~~~~~~~
# | 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... |