Submission #750445

#TimeUsernameProblemLanguageResultExecution timeMemory
750445nguyenneheheKnapsack (NOI18_knapsack)C++14
100 / 100
57 ms3864 KiB
#include<bits/stdc++.h> using namespace std; signed main() { cin.tie(nullptr)->sync_with_stdio(false); int s, n; cin >> s >> n; vector<vector<pair<int, int>>> items(s + 1); for (int i = 0; i < n; ++i) { int v, w, k; cin >> v >> w >> k; items[w].emplace_back(v, k); } vector<pair<int, int>> a; for (int w = 1; w <= s; ++w) { sort(items[w].begin(), items[w].end(), greater<>()); int cnt = s / w; for (pair<int, int> p: items[w]) { int bound = min(p.second, cnt); for (int i = 1; i <= bound; ++i) { a.emplace_back(w, p.first); } cnt -= bound; if (cnt == 0) break; } } vector<long long> dp(s + 1); for (pair<int, int> p: a) { for (int w = s; w >= p.first; --w) { dp[w] = max(dp[w], dp[w - p.first] + p.second); } } cout << *max_element(dp.begin(), dp.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...