Submission #967534

#TimeUsernameProblemLanguageResultExecution timeMemory
967534stdfloatKnapsack (NOI18_knapsack)C++14
73 / 100
299 ms262144 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

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

	int S, n;
	cin >> S >> n;

	vector<vector<pair<int, int>>> u(S + 1);
	while (n--) {
		int v, w, k;
		cin >> v >> w >> k;

		u[w].push_back({v, k});
	}
	
	vector<int> V, W;
	for (int i = 1; i <= S; i++) {
		sort(u[i].rbegin(), u[i].rend());

		int s = S / i;
		for (auto [v, k] : u[i]) {
			int mn = min(s, k);

			while (mn--) {
				V.push_back(v);
				W.push_back(i);
			}

			s -= mn;
		}
	}


	vector<ll> dp(S + 1, -1);
	dp[0] = 0;
	for (int i = 0; i < (int)V.size(); i++) {
		for (int j = S; j >= W[i]; j--)
			if (dp[j - W[i]] != -1) dp[j] = max(dp[j], dp[j - W[i]] + V[i]);
	}

	cout << *max_element(dp.begin(), dp.end());
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:25:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   25 |   for (auto [v, k] : u[i]) {
      |             ^
#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...