제출 #1323757

#제출 시각아이디문제언어결과실행 시간메모리
1323757namangarg5432Knapsack (NOI18_knapsack)C++20
29 / 100
1 ms1848 KiB
#include <bits/stdc++.h> using namespace std; int main() { int n,s; cin >> s >> n; vector<int>arr(n); vector<int>wei(n); vector<int>qua(n); for(int i = 0;i < n;i++) { cin >> arr[i] >> wei[i] >> qua[i]; } vector<vector<pair<int,int>>>dp(n,vector<pair<int,int>>(s + 1,{0,0})); for(int w = 0;w <= s;w++) { int a = min(qua[0],(w/wei[0])); dp[0][w] = {a*arr[0],a}; } for(int i = 1;i < n;i++) { for(int w = 0;w <= s;w++) { int take = 0; int q = 0; if(wei[i] <= w) { q = dp[i][w - wei[i]].second; if(q < qua[i]) { take = arr[i] + dp[i][w - wei[i]].first; } else { take = arr[i] + dp[i - 1][w - wei[i]].first; q = 0; } } int nottake = dp[i - 1][w].first; if(take > nottake) { dp[i][w] = {take,q + 1}; } else { int q2 = 0; dp[i][w] = {nottake,q2}; } } } cout<<dp[n - 1][s].first<<endl; 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...