Submission #669825

#TimeUsernameProblemLanguageResultExecution timeMemory
669825rv4102Knapsack (NOI18_knapsack)C++17
73 / 100
1096 ms1460 KiB
#include <iostream> #include <vector> using namespace std; int main(){ int capacity, n; cin >> capacity >> n; vector<int> price(n), weight(n), copies(n); for(int i=0; i<n; i++){ cin >> price[i] >> weight[i] >> copies[i]; } // // dp[i][j] tells us the maximum profit we can achieve using // // a subcollection of first i elements, with j weight // vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(2001, vector<int>(capacity+1, 0))); // for(int i=0; i<n; i++){ // for(int j=1; j<=2000; j++){ // for(int k=1; k<=capacity; k++){ // if(k >= weight[i]){ // dp[i][j][k] = max(dp[i][j-1][k], dp[i][j-1][k-weight[i]] + price[i]); // } // else{ // dp[i][j][k] = dp[i][j-1][k]; // } // } // } // } vector<int> dp(capacity+1, 0); for(int i=0; i<n; i++){ int max_copies = capacity / weight[i]; for(int j=1; j<=min(max_copies, copies[i]); j++){ for(int wt=capacity; wt>=0; wt--){ if(wt >= weight[i]) dp[wt] = max(dp[wt], dp[wt-weight[i]] + price[i]); else break; } } } // int ans=0; // for(int i=0; i<=2000; i++) // ans = max(ans, dp[n-1][i][capacity]); // cout << ans << endl; cout << dp[capacity] << 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...