제출 #465058

#제출 시각아이디문제언어결과실행 시간메모리
465058Qw3rTyKnapsack (NOI18_knapsack)C++11
100 / 100
143 ms8644 KiB
#include <bits/stdc++.h>
#define int long long
#define pi pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;

const int maxN = 1e5+5;
const int maxV = 2e3+5;

vector<int> d[maxV];
int S,N;
int f[maxV];

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("../test.in","r",stdin);
	cin >> S >> N;
	for(int i = 1; i <= N; ++i){
		int v,w,k; cin >> v >> w >> k;
		for(int j = 1; j <= k; j <<= 1){
			if(w * j > S)break;
			k -= j;
			d[w*j].pb(v*j);
		}
		if(k > 0 && w * k <= S)d[w*k].pb(v*k);
	}
	for(int i = 1; i <= S; ++i){
		if(d[i].size()){ //for each group of objects w/ weight = i
			sort(d[i].begin(),d[i].end(),greater<int>());
			//Consider only the first floor(S/W) objects
			int num = min(S/i,(int)(d[i].size()));
			for(int j = 0; j < num; ++j){
				for(int k = S; k >= i; --k){
					f[k] = max(f[k],f[k-i] + d[i][j]);
				}
			}
		}
	}
	cout << f[S] << '\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...