Submission #551921

#TimeUsernameProblemLanguageResultExecution timeMemory
551921training4usacoKnapsack (NOI18_knapsack)C++11
100 / 100
116 ms3708 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define pii pair<int, int> #define fi first #define se second #define INF 1000000009 #define MAXS 2005 vector<pii> weightgroup[MAXS]; int dp[MAXS]; bool comp(pii a, pii b) { return a.fi > b.fi; } int main() { int s, n; cin >> s >> n; for(int i = 0; i < n; ++i) { int v, w, k; cin >> v >> w >> k; weightgroup[w].push_back(make_pair(v, k)); } for(int i = 1; i <= s; ++i) { sort(weightgroup[i].begin(), weightgroup[i].end(), comp); } for(int i = 0; i <= s; ++i) { dp[i] = -INF; } dp[0] = 0; for(int i = 1; i <= s; ++i) { int usedspace = 0; for(pii item : weightgroup[i]) { for(int c = 1; c <= item.se; ++c) { usedspace += i; for(int leftover = s; leftover >= usedspace; --leftover) { dp[leftover] = max(dp[leftover], dp[leftover - i] + item.fi); } if(usedspace > s) { break; } } if(usedspace > s) { break; } } } int answer = 0; for(int i = 1; i <= s; ++i) { answer = max(answer, dp[i]); } cout << answer << endl; }
#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...