This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://oj.uz/problem/view/NOI18_knapsack
#include <bits/stdc++.h>
#define loop(i, l, r) for(int i = (l); i < (r); ++i)
#define pool(i, l, r) for(int i = (l); i > (r); --i)
#define debug(x) cerr << #x << ": " << x << endl;
using namespace std;
int n,s;
const int MAXS = 2005;
long long dp[MAXS];
map<int,vector<pair<int,int>>> items;
bool cmp(pair<int,int> a,pair<int,int> b){
return a.first>b.first;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>s>>n;
long long v,w,k;
loop(i,0,n){
cin>>v>>w>>k;
items[w].push_back({v,k});
}
for(auto& i:items){
vector<pair<int,int>>& a = i.second;
sort(a.begin(),a.end(),cmp);
pool(j,s,-1){
int value = 0;
int c = 0;
int count = 0;
int l = 1;
while(c<a.size()){
w = l*i.first;
if(j>=w){
value+=a[c].first;
count++;
if(count>=a[c].second){
c++;
count = 0;
}
dp[j] = max(dp[j],dp[j-w]+value);
}else{
break;
}
l++;
}
}
}
cout<<dp[s]<<endl;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:34:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(c<a.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... |