Submission #950334

#TimeUsernameProblemLanguageResultExecution timeMemory
950334irmuunKnapsack (NOI18_knapsack)C++17
100 / 100
54 ms5360 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define ff first #define ss second #define all(s) s.begin(),s.end() #define rall(s) s.rbegin(),s.rend() int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll s,n; cin>>s>>n; ll dp[s+5]; fill(dp,dp+s+1,0); vector<pair<ll,ll>>q[s+5]; for(ll i=1;i<=n;i++){ ll v,w,k; cin>>v>>w>>k; q[w].pb({v,k}); } vector<pair<ll,ll>>item; for(ll i=1;i<=s;i++){ sort(all(q[i])); for(ll j=0;j<s/i;j++){ if(q[i].size()==0) break; item.pb({i,q[i].back().ff}); q[i].back().ss--; if(q[i].back().ss==0) q[i].pop_back(); } } for(auto [w,v]:item){ for(ll j=s-w;j>=0;j--){ dp[j+w]=max(dp[j+w],dp[j]+v); } } ll ans=0; for(ll i=0;i<=s;i++){ ans=max(ans,dp[i]); } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...