제출 #718606

#제출 시각아이디문제언어결과실행 시간메모리
718606izanbfKnapsack (NOI18_knapsack)C++14
100 / 100
356 ms3420 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int S, N;
	cin >> S >> N;

	vector<int> dp(S+1, 0);

	map<int,vector<pair<int,int>>> m; 

	for (int i = 0; i < N; ++i) {
		int v, w, k;
		cin >> v >> w >> k;
		m[w].push_back({v, k});
	}

	for (auto& it : m) {
		sort(it.second.begin(), it.second.end());
		reverse(it.second.begin(), it.second.end());
	}

	for (auto& it : m) {
		int w = it.first;
		for (int s = S; s >= 0; --s) {
			int prev_k = 0;
			int prev_v = 0;
			for (auto& p : it.second) {
				int v = p.first;
				int k = p.second;
				for (int j = 1; j <= k and (prev_k+j)*w <= s; ++j) {
					dp[s] = max(dp[s], dp[s-(prev_k+j)*w] + prev_v + j*v);
				}
				if (k > S) break;
				prev_k += k;
				prev_v += k*v;
			}
		}
	}

	cout << dp[S] << endl;
}
#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...