Submission #887592

#TimeUsernameProblemLanguageResultExecution timeMemory
887592_saketks_07Knapsack (NOI18_knapsack)C++14
100 / 100
65 ms6596 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; map<int, int> sum; const int mxN = 2*1e5+10; ll dp[mxN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int s, n; cin >> s >> n; vector<array<ll, 3>> v; for(int i = 0; i < n; i++) { ll val, w, k; cin >> val >> w >> k; v.push_back({val, w, k}); } sort(v.rbegin(), v.rend()); vector<array<ll, 2>> a; for(auto it : v) { ll val = it[0], w = it[1], k = it[2]; while(sum[w]+w <= s && k) { sum[w] += w; k--; a.push_back({w, val}); } } for(auto it : a) { for(int x = s; x >= 0; x--) { if(x-it[0] >= 0) { dp[x] = max(dp[x], dp[x-it[0]]+it[1]); } } } ll ans = 0; for(int i = 1; i <= s; i++) { ans = max(ans, dp[i]); } cout << ans; 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...