Submission #720449

#TimeUsernameProblemLanguageResultExecution timeMemory
720449mseebacherKnapsack (NOI18_knapsack)C++17
100 / 100
138 ms5248 KiB
#include <bits/stdc++.h> 
 
using namespace std;
typedef long long ll;
 
struct Produkt{
	ll v,w,k;
};
 
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cout << fixed << setprecision(8);
 
    int s,n; cin >> s >> n;
	
	vector<priority_queue<pair<ll,ll>>> weights(s+1);
	vector<Produkt> p;
	for(int i = 0;i<n;i++){
		Produkt pt;
		cin >> pt.v >> pt.w >> pt.k;
		weights[pt.w].push({pt.v,pt.k});
	}
	for(int i = 1;i<=s;i++){
		ll anzahl = s/i;
		while(anzahl && !weights[i].empty()){
			pair<ll,ll> f = weights[i].top(); weights[i].pop();
			Produkt px; px.w = i; px.v = f.first;
			for(int j = 0;j<min(anzahl,f.second);j++){
				p.push_back(px);
			}
			anzahl -= min(anzahl, f.second);
		}
	}
	vector<ll> dp(s+1,0);
	ll ans = 0;
	for(int i = 0;i<(int)p.size();i++){
		for(int j = p[i].w;j<=s;j++){
			dp[j-p[i].w] = max(dp[j-p[i].w],dp[j] + p[i].v);
			ans = max(ans,dp[j-p[i].w]);
		}
	}
	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...