Submission #476576

#TimeUsernameProblemLanguageResultExecution timeMemory
476576BERNARB01Knapsack (NOI18_knapsack)C++17
100 / 100
86 ms3712 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<vector<pair<int, int>>> v(s + 1); for (int i = 0; i < n; i++) { int vv, w, k; cin >> vv >> w >> k; v[w].emplace_back(vv, k); } vector<int> a, w; for (int i = 1; i <= s; i++) { sort(v[i].rbegin(), v[i].rend()); for (int j = 0, k = 0; k < (int) v[i].size() && j < s / i; j++) { v[i][k].second -= 1; a.push_back(v[i][k].first); w.push_back(i); if (v[i][k].second == 0) { k++; } } } vector<int> dp(s + 1, 0); n = a.size(); for (int i = 0; i < n; i++) { for (int j = s; j >= w[i]; j--) { dp[j] = max(dp[j], a[i] + dp[j - w[i]]); } } cout << *max_element(dp.begin(), dp.end()) << '\n'; return 0; }
#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...