제출 #937722

#제출 시각아이디문제언어결과실행 시간메모리
937722hengliaoKnapsack (NOI18_knapsack)C++17
100 / 100
44 ms4972 KiB
#include<bits/stdc++.h> using namespace std; #define F first #define S second #define pb push_back #define vll vector<ll> #define pll pair<ll, ll> typedef long long ll; const ll mxN=2005; vector<pair<ll, ll>> pos[mxN]; ll dp[mxN]; bool compare(pll a, pll b){ return a.F>b.F; } void solve(){ memset(dp, 0, sizeof(dp)); ll n, s; cin>>s>>n; for(ll i=0;i<n;i++){ ll v, w, k; cin>>v>>w>>k; pos[w].pb({v, k}); } for(ll i=1;i<mxN;i++){ sort(pos[i].begin(), pos[i].end(), compare); } for(ll w=1;w<=s;w++){ ll cnt=0; pll lef={0, 0}; ll mx=s/w; for(auto &[v, num]:pos[w]){ if(cnt+num>mx){ lef={v, mx-cnt}; break; } cnt+=num; for(ll rep=0;rep<num;rep++){ for(ll i=s;i>=w;i--){ dp[i]=max(dp[i], dp[i-w]+v); } } } if(lef.S!=0){ ll num=lef.S; for(ll rep=0;rep<num;rep++){ for(ll i=s;i>=w;i--){ dp[i]=max(dp[i], dp[i-w]+lef.F); } } } } ll ans=0; for(ll i=1;i<=s;i++){ ans=max(ans, dp[i]); } cout<<ans<<'\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#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...