Submission #732034

#TimeUsernameProblemLanguageResultExecution timeMemory
732034manhtuan22007Knapsack (NOI18_knapsack)C++14
100 / 100
60 ms5000 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int n , s , dp[2005]; vector<pair<int , int>> a[2005]; int32_t main() { cin.tie(0)->sync_with_stdio(0); cin >> s >> n; for(int i = 1 ; i <= n ; i ++){ int v , w , k; cin >> v >> w >> k; a[w].push_back({v , k}); // s[w] += k; } for(int i = 1 ; i <= s ; i ++){ if(!a[i].empty()) sort(a[i].begin() , a[i].end() , greater<pair<int , int>>()); } for(int i = 1 ; i <= s ; i ++){ if(a[i].empty()) continue; int ind = 1 , sum = 1 , j = 1; while(sum <= s / i && ind <= (int)a[i].size()){ if(a[i][ind - 1].second < j){ ++ind; j = 1; } if(ind > (int)a[i].size()) continue; for(int c = s ; c >= i ; c --){ dp[c] = max(dp[c] , dp[c - i] + a[i][ind - 1].first); } ++sum , ++j; } } cout << dp[s]; }
#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...