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;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define N 2005
long long dp[N],ans;
vector<pair<int,int>> g[N],op;
int main(){
int n,s,i,v,w,k,cnt;
scanf("%d %d",&s,&n);
for(i=1;i<=n;i++){
scanf("%d %d %d",&v,&w,&k);
g[w].push_back({v,k});
}
for(i=1;i<=s;i++)sort(g[i].begin(),g[i].end(),greater<pair<int,int>>());
for(i=1;i<=s;i++){
cnt=0;
for(auto [v,k]:g[i]){
if(cnt+k<=s)while(k--)op.push_back({v,i});
else{
k=s-cnt;
cnt=s;
while(k--)op.push_back({v,i});
}
if(cnt==s)break;
}
}
for(auto [v,w]:op)for(i=s;i>=w;i--)dp[i]=max(dp[i],dp[i-w]+v);
for(i=0;i<=s;i++)ans=max(ans,dp[i]);
printf("%lld",ans);
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | scanf("%d %d",&s,&n);
| ~~~~~^~~~~~~~~~~~~~~
knapsack.cpp:15:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d %d %d",&v,&w,&k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |