Submission #987545

#TimeUsernameProblemLanguageResultExecution timeMemory
987545vjudge1Knapsack (NOI18_knapsack)C++17
12 / 100
1 ms604 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" int n, m; ll b[2005]; vector<vector<pair<int, int>>> a; ll dp[2005]; 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 <= n; i++) { sort(a[i].begin(), a[i].end()); } for (int i = 1; i <= m; i++) { int dem = m / i; for (auto &[v, k] : a[i]) { while (k != 0 && dem != 0) { c.push_back({i, v}); k--; dem--; } } } 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...