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 fast(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
map<int, vector<pair<int,int>>> mp;
int main(){
fast();
int m, n; cin>>m>>n;
for(int i=1; i<=n; i++){
int v, w, k; cin>>v>>w>>k;
if(w<=m) mp[w].push_back({v, k});
}
vector<long long> dp(m+1);
dp[0]=0;
for(auto &[w, items]:mp){
sort(items.begin(), items.end(), greater<pair<int,int>>());
for(int i=m; i>=1; i--){
int cop=0, copnow=0, idx=0;
long long val=0;
while(idx<items.size() && (cop+1)*w<=i){
cop++;
copnow++;
val+=items[idx].first;
dp[i]=max(dp[i], dp[i-cop*w]+val);
if(copnow==items[idx].second){
copnow=0;
idx++;
}
}
}
}
cout<<dp[m];
}
/*
const int N=2e3;
long long dp[N+1];
int main(){
fast();
int s, n, v, w, k; cin>>s>>n;
for(int i=1; i<=n; i++){
cin>>v>>w>>k;
for(int j=s; j>=1; j--){
for(int l=1; l<=k; l++){
if(j<w*l) break;
dp[j]=max(dp[j], dp[j-w*l]+1ll*v*l);
}
}
}
cout<<dp[s];
}*/
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:20:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | while(idx<items.size() && (cop+1)*w<=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... |