Advertisement
Falak_Ahmed_Shakib

(0,1) KnapSack

Nov 28th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<ll,ll> pll;
  5. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. ll const MOD=1000000007;
  10. #define eb emplace_back
  11. const int N=10000005;
  12.  
  13. int main()
  14. {
  15.  
  16. ll n,m;
  17.  
  18. cin>>n>>m;
  19.  
  20.  
  21. ll p[n+1],wt[n+1];
  22.  
  23. p[0]=wt[0]=0;
  24.  
  25.  
  26. for(ll i=1;i<=n;i++)
  27. {
  28. cin>>wt[i]>>p[i];
  29.  
  30. }
  31.  
  32.  
  33. ll dp[n+1][m+1];
  34.  
  35. for(ll i=0;i<=n;i++)
  36. {
  37.  
  38. for(ll w=0;w<=m;w++)
  39. {
  40. if(i==0 || w==0)dp[i][w]=0;
  41.  
  42. else if(wt[i]<=w)
  43. {
  44.  
  45. dp[i][w]=max(p[i]+dp[i-1][w-wt[i]],dp[i-1][w]);
  46.  
  47.  
  48. }else dp[i][w]=dp[i-1][w];
  49.  
  50.  
  51.  
  52. }
  53.  
  54.  
  55. }
  56.  
  57. cout<<dp[n][m]<<endl;
  58.  
  59.  
  60.  
  61.  
  62.  
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement