Submission #1131529

#TimeUsernameProblemLanguageResultExecution timeMemory
1131529lopkusKnapsack (NOI18_knapsack)C++20
100 / 100
60 ms5572 KiB
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int s, n; cin >> s >> n; vector<int> w(n + 1); vector<int> v(n + 1); vector<int> k(n + 1); for(int i = 1; i <= n; i++) { cin >> v[i] >> w[i] >> k[i]; } vector<pair<int,int>> adj[s + 1]; for(int i = 1; i <= n; i++) { adj[w[i]].push_back({v[i], k[i]}); } vector<pair<int,int>> u; for(int i = 1; i <= s; i++) { int cnt = s / i; sort(adj[i].begin(), adj[i].end()); reverse(adj[i].begin(), adj[i].end()); for(auto it : adj[i]) { if(cnt < 0) { break; } for(int j = 1; j <= it.second; j++) { if(cnt < 0) { break; } u.push_back({it.first, i}); cnt -= 1; } } } vector<int> dp(s + 1,0); vector<int> new_dp(s + 1,0); new_dp[u[0].second] = u[0].first; for(int i = 1; i < u.size(); i++){ for(int j = 0; j <= s; j++)dp[j] = new_dp[j]; for(int j = u[i].second; j <= s; j++)dp[j] = max( dp[j], new_dp[j - u[i].second] + u[i].first); swap(dp, new_dp); } int ans = 0; for(int i = 1; i <= s; i++) ans = max( ans, new_dp[i]); cout << ans; }
#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...