Submission #987548

#TimeUsernameProblemLanguageResultExecution timeMemory
987548vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
54 ms3920 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" int n, m; vector<vector<pair<int, int> > > a; ll dp[20005]; vector<pair<int, int> > c; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> m >> n; a.assign(m + 1, {}); for (int i = 1; i <= n; i++) { int v, w, k; cin >> v >> w >> k; a[w].push_back({v, k}); } for (int i = 1; i <= m; i++) { sort(a[i].begin(), a[i].end(), greater<pair<int,int> >()); } for (int i = 1; i <= m; i++) { int dem = m / i; for (auto [v, k] : a[i]) { if (dem == 0) break; while (k != 0 && dem != 0) { c.push_back({i, v}); k--; dem--; } } } // for (auto [i, v] : c) // { // cout << i << " "; // } // cout << endl; for (auto [i, v] : c) { for (int j = m; j >= i; j--) { if (dp[j - i] != 0 || j == i) dp[j] = max(dp[j], dp[j - i] + v); } } cout << *max_element(dp + 1, dp + m + 1); }
#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...