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;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int s,n;
cin >> s >> n;
vector<vector<pair<int, int>>> items(s+1);
for(int i = 0; i < n; i++) {
int value, weight, number;
cin >> value >> weight >> number;
items[weight].push_back(make_pair(value, number));
}
for(int i = 1; i <= s; i++) {
sort(begin(items[i]), end(items[i]), [](const pair<int, int> a, const pair<int, int> b) -> bool{
return a.first>b.first;
});
}
ll arr[s+1][s+1];
//arr[a][b]=x: first a weights, b total weight, x max value
for(int i = 0; i <= s; i ++) {
for(int j = 0; j <= s; j++) {
arr[i][j]=0;
}
}
for(int i = 1; i <= s; i++) {
for(int j = 0; j <= s; j++) {
arr[i][j]=max(arr[i-1][j], arr[i][j]);
if(items[i].size()==0) continue;
int weight = j;
ll value = arr[i-1][j];
int itemsUsed = 0;
int index = 0;
while(weight+i<=s) {
itemsUsed++;
weight+=i;
value+=items[i][index].first;
arr[i][weight]=max(arr[i][weight], value);
if(itemsUsed==items[i][index].second) {
itemsUsed=0;
index++;
if(index==items[i].size()) {
break;
}
}
}
}
}
ll ans = 0;
for(int i = 0; i <= s; i++) {
ans=max(ans, arr[s][i]);
}
cout << ans << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:44:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if(index==items[i].size()) {
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |