Submission #749187

#TimeUsernameProblemLanguageResultExecution timeMemory
749187cristi_aKnapsack (NOI18_knapsack)C++17
100 / 100
291 ms17408 KiB
#include <bits/stdc++.h> using namespace std; const int nmax = 1e6; const int smax = 2e3; int dp[smax+5][smax+5]; int main() { ios::sync_with_stdio(0); cin.tie(0); int s, n; cin >> s >> n; vector<pair<int, int>> cost[smax+5]; for(int i=1; i<=n; i++) { int v, w, k; cin >> v >> w >> k; cost[w].emplace_back(v, k); } for(auto &i : cost) sort(i.begin(), i.end(), greater<pair<int, int>>()); for(int i=0; i<=s; i++) { for(int j=1; j<=i; j++) { int taken = 0, val = 0; dp[i][j] = dp[i][j-1]; for(auto item : cost[j]) { for(int ct=0; ct<item.second and (taken+1)*j<=i; ++ct) { taken++; val += item.first; dp[i][j] = max(dp[i][j], dp[i-taken*j][min(j-1,i-taken*j)]+val); } } } } cout << dp[s][s]; 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...