fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int knapsack(int cap,vector<int> wt, vector<int> pro,int n) {
  5. if(n==0 || cap==0) return 0;
  6. if(wt[n-1] > cap) return knapsack(cap,wt,pro,n-1);
  7. return max(knapsack(cap,wt,pro,n-1),
  8. pro[n-1] + knapsack(cap-wt[n-1],wt,pro,n-1)
  9. );
  10.  
  11. }
  12. int main() {
  13. vector<int> weight = {3,2,4};
  14. vector<int> profit = {6,8,7};
  15. int capacity = 8;
  16. cout<<knapsack(capacity,weight,profit,profit.size());
  17. return 0;
  18. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
15