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 main(){
int n, s, v, w, num;
cin>>s>>n;
map<int, vector<pair<int,int>>> by_weight;
for(int i=0;i<n;i++){
cin>>v>>w>>num;
if(w <= s && num > 0) by_weight[w].push_back({v, num});
}
vector<vector<long long>> dp(by_weight.size()+1, vector<long long> (s+1, INT_MIN));
int at = 1;
dp[0][0] = 0;
for(auto &[w, item] : by_weight){
sort(item.begin(), item.end(), greater<pair<int, int>>());
for(int i=0;i<=s;i++){
dp[at][i] = dp[at-1][i];
int ind = 0;
num = 0;
int cur_in = 0;
long long price = 0;
while(ind < item.size() && w * (num+1) <= i){
num++;
price += item[ind].first;
if(dp[at-1][i-w * num] != INT_MIN) dp[at][i] = max(dp[at][i], dp[at-1][i-w * num] + price);
cur_in++;
if(cur_in == item[ind].second) cur_in = 0, ind++;
}
}
at++;
}
cout<<*max_element(dp.back().begin(), dp.back().end());
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:23:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | while(ind < item.size() && w * (num+1) <= i){
| ~~~~^~~~~~~~~~~~~
# | 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... |