Submission #916198

#TimeUsernameProblemLanguageResultExecution timeMemory
916198lamterKnapsack (NOI18_knapsack)C++14
73 / 100
1073 ms1956 KiB
#include <bits/stdc++.h>

int main(void) {
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(nullptr);

	int S, n; std::cin >> S >> n;
	std::vector <std::vector <std::pair <int, int>>> items(S + 1);
	for (int i = 0; i < n; i += 1) {
		int v, w, k; std::cin >> v >> w >> k;
		items[w].emplace_back(v, std::min(k, S / w));
	}

	std::vector <long long int> dp(S + 1);
	for (auto& it : items) {
		std::sort(it.begin(), it.end(), std::greater <> ());
	}

	for (int w = 1; w <= S; w += 1) {
		for (const auto& [v, cnt] : items[w]) {
			int total = 0;
			for (int i = 1; i <= cnt and total <= S; i += 1) {
				for (int t = S; t >= w; t -= 1)
					dp[t] = std::max(dp[t], dp[t - w] + v);
				total += w;
			}
		}
	}

	std::cout << dp.back() << '\n';

	return 0^0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:20:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   20 |   for (const auto& [v, cnt] : items[w]) {
      |                    ^
#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...