제출 #1128605

#제출 시각아이디문제언어결과실행 시간메모리
1128605rcllKnapsack (NOI18_knapsack)C++17
100 / 100
128 ms33280 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	int s,n;
	cin >> s >> n;
	map<int,vector<pair<int,int>>> by_weight;
	for (int t = 0; t < n; t++) {
		int value;
		int weight;
		int amt;
		cin >> value >> weight >> amt;
		if (weight<=s && amt>0) { 
			by_weight[weight].push_back({value, amt}); 
		}
	}
	vector<vector<long long>> best(by_weight.size()+1,vector<long long>(s+1,INT32_MIN));
	best[0][0] = 0;
	int at = 1;
	for (auto &[w, items]:by_weight) {
		sort(items.begin(), items.end(), greater<pair<int, int>>());
		for (int i=0; i<=s; i++) {
			best[at][i] = best[at - 1][i];
			int copies = 0;
			int type_at = 0;
			int curr_used = 0;
			long long profit = 0;
			while ((copies + 1) * w <= i && type_at < items.size()) {
				copies++;
				profit += items[type_at].first;
				if (best[at - 1][i - copies * w] != INT32_MIN) {
					best[at][i] =
					    max(best[at][i], best[at - 1][i - copies * w] + profit);
				}

				curr_used++;
				if (curr_used==items[type_at].second) {
					curr_used=0;
					type_at++;
				}
			}
		}
		at++;
	}
	cout << *max_element(best.back().begin(),best.back().end());
}
#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...