Submission #1192530

#TimeUsernameProblemLanguageResultExecution timeMemory
1192530dprtoKnapsack (NOI18_knapsack)C++20
100 / 100
39 ms5192 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define x first #define y second #define MASK(i) (1 << (i)) #define BIT(x, i) (((x) >> (i)) & 1) #define f(i, l, r) for(int i = l; i <= r; ++i) const int mod = 1e9 + 7; const int ars = 1e5 + 5; const ll infll = 1e18; int s, n, v[ars], w[ars], k[ars], dp[ars]; vector<pair<int, int>> adj[ars]; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s >> n; for(int i = 1; i <= n; ++i){ cin >> v[i] >> w[i] >> k[i]; if(w[i] > s) continue; adj[w[i]].push_back({v[i], k[i]}); } for(int i = 1; i <= s; ++i) dp[i] = -1e9; dp[0] = 0; for(int w = 1; w <= s; ++w){ if(adj[w].empty()) continue; sort(adj[w].begin(), adj[w].end(), greater<>()); vector<int> items; int limit = s / w; for(auto [val, cnt] : adj[w]){ int take = min(cnt, limit); limit -= take; while(take--) items.push_back(val); if(limit == 0) break; } for(int v : items){ for(int i = s - w; i >= 0; --i){ if(dp[i] != -1e9){ dp[i + w] = max(dp[i + w], dp[i] + v); } } } } int res = 0; for(int i = 1; i <= s; ++i){ res = max(res, dp[i]); } cout << res << "\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...