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>
#define mp make_pair
using namespace std;
const int N=2e3+10;
vector<pair<int,int> > v;
vector<pair<int,pair<int,int> > > inp;
int dp[N],wg[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int s,n;
cin>>s >>n;
for(int i=0;i<n;i++){
int a,b,c;
cin>>a >>b >>c;
inp.push_back(mp(a,mp(b,c)));
//value,weight,number
}
sort(inp.begin(),inp.end(),greater<pair<int,pair<int,int> > >());
for(int i=0;i<n;i++){
int val=inp[i].first,w=inp[i].second.first,num=inp[i].second.second;
if(w>s) continue;
while(wg[w]+w<=s && num){
num--;
wg[w]+=w;
v.push_back(mp(val,w));
}
}
for(int i=0;i<v.size();i++){
int val=v[i].first,w=v[i].second;
for(int j=s;j>=1;j--){
if(j-w>=0) dp[j]=max(dp[j],dp[j-w]+val);
}
}
cout<<dp[s];
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:29:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int i=0;i<v.size();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... |