Submission #1036837

#TimeUsernameProblemLanguageResultExecution timeMemory
1036837ocasuKnapsack (NOI18_knapsack)C++17
100 / 100
53 ms8072 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int inf=1e12; signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int S,n; cin>>S>>n; vector<vector<int>> a(n); for (int i=0; i<n; i++){ int v,w,k; cin>>v>>w>>k; a[i]={v,w,k}; } sort(a.begin(),a.end(),greater<vector<int>>()); vector<vector<int>> items(S+1); for (int i=0; i<n; i++){ int v=a[i][0], w=a[i][1], k=a[i][2]; while (k-- and ((int)(items[w].size())<S/w+1)) items[w].push_back(v); } vector<int> dp(S+1,-inf); //current maximum value of a subset with total weight w dp[0]=0; for (int w=0; w<=S; w++){ for (auto v: items[w]){ for (int i=S; i>=w; i--){ dp[i]=max(dp[i], dp[i-w]+v); } } } int ans=0; for (auto i: dp) ans=max(ans, i); cout<<ans<<'\n'; }
#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...