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;
void solve(){
int S,N;
cin>>S>>N;
map<long long, vector<vector<long long>>> m;
for(int i=1;i<=N;i++) {
long long v,w,k;
cin>>v>>w>>k;
m[w].push_back({v,k});
}
vector<vector<long long>> dp(2,vector<long long>(S+1));
int cur=1;
int last=0;
for(auto[w,vec]: m){
sort(vec.begin(),vec.end());
reverse(vec.begin(),vec.end());
// cout<<"weight: "<<w<<'\n';
// for(auto x:vec) cout<<x[0]<<' '<<x[1]<<'\n';
// cout<<'\n';
for(int j=S;j>=0;j--){
dp[cur][j] = dp[last][j];
int curitem=0;
int copy=0;
int tot_weight=0;
long long profit=0;
while(j>=(tot_weight+1)*w && curitem<vec.size()){
tot_weight++;
profit += vec[curitem][0];
dp[cur][j] = max(dp[cur][j], dp[last][j-tot_weight*w]+profit);
copy++;
if (copy==vec[curitem][1]){
curitem++;
copy=0;
}
}
}
swap(cur,last);
}
// for(auto x:dp){
// for(auto y:x)cout<<y<<' ';
// cout<<'\n';
// }
cout<<dp[m.size()%2][S]<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
while(t--) solve();
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:34:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(j>=(tot_weight+1)*w && curitem<vec.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... |