Submission #1183529

#TimeUsernameProblemLanguageResultExecution timeMemory
1183529bgnbvnbvKnapsack (NOI18_knapsack)C++20
17 / 100
1 ms328 KiB
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long

#define st first
#define nd second
#define int long long
using namespace std;

const int lim = 2e5+1;
const ll mod = 1e9+7;
const int base = 31;
const int maxn = 1e5+5;
const int inf = 1e18;




main() {
   	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	
	int n,s; cin >> s >> n;
	
	vector <vector <pair<int,int> > > a(s+1);
	
	for(int i = 0; i <n;++i){
		int v,w,k;
		cin >> v >> w>>k;
		a[w].pb({v,k});
	}
	
	
	vector <int> dp(s+1,0);
	
	
	for(int x = 1 ; x <= s;++x){
		if(a[x].empty()) continue;
		
		vector <int> item;
		
		for(auto [v,k] : a[x]){
			
			for(int use = 1; k >0; use *= 2){
				int rem = min(use,k);
				item.pb(v * rem);
				k -= rem;
			}
		}
		
		int mx = s/x;
		if(item.size() > mx){
			nth_element(item.begin(),item.begin() + mx,item.end(),greater<>());
			item.resize(mx);
		}
		
		for(int v : item){
			for(int j = s; j >= x;--j){
				dp[j] = max(dp[j],dp[j-x] + v);
			}
		}
		
		
		
	}
	
	cout << dp[s] << endl;
	
	
    return 0;
}

Compilation message (stderr)

knapsack.cpp:21:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   21 | main() {
      | ^~~~
#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...