Misbah_Uddin_Tareq

0-1 Knapsac(DP)

Feb 5th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. #define pb push_back
  5. const ll mx=100001;
  6.  
  7. ll cost[mx],weight[mx];
  8. ll n,cap;
  9.  
  10. ll func(ll i, ll w)
  11. {
  12.     ll profit1,profit2;
  13.     if(i==n)
  14.         return 0;
  15.     if(w+weight[i]<=cap)
  16.         profit1=cost[i]+func(i+1,w+weight[i]);
  17.     else
  18.         profit1=0;
  19.     profit2=func(i+1,w);
  20.  
  21.     return max(profit1,profit2);
  22. }
  23.  
  24.  
  25.  
  26. int main()
  27. {
  28.     cin>>n>>cap;
  29.  
  30.     for(int i=0; i<n; i++)
  31.         cin>>weight[i]>>cost[i];
  32.  
  33.     cout<<func(0,0)<<endl;
  34.  
  35.     return 0;
  36. }
Add Comment
Please, Sign In to add comment