Advertisement
LEGEND2004

Knapsack 1

Aug 29th, 2023
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int mod = 1e9 + 7;
  6. const int N = 1e5 + 5;
  7.  
  8. int n , w;
  9. int dp[N];
  10. int a[N];
  11. int b[N];
  12.  
  13. signed main()
  14. {
  15.     cin >> n >> w;
  16.     for(int i = 0; i < n; i++){
  17.         cin >> a[i] >> b[i];
  18.     }
  19.     dp[0] = 0;
  20.     for(int j = 0; j < n; j++){
  21.         for(int i = w; i >= 1; i--){
  22.             if(i < a[j])
  23.                 continue;
  24.             dp[i] = max(dp[i] , dp[i - a[j]] + b[j]);
  25.         }
  26.     }
  27.     cout << dp[w] << endl;
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement