Advertisement
El_GEMMY

knapsack (Memoization)

Mar 26th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. // Those who cannot remember the past are
  2. // condemned to repeat it (use DP -_-)
  3. // - George Santayana
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. #define all(v) v.begin(),v.end()
  13. #define rall(v) v.rbegin(),v.rend()
  14. #define ll long long
  15. #define ull unsigned long long
  16. #define MOD 1000000007
  17. #define PI 3.14159265
  18. #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  19. #define imin INT_MIN
  20. #define imax INT_MAX
  21. #define inf 2000000000
  22. #define nl '\n'
  23. #define modulo(a, b, mod) ((((a) % mod) * ((b) % mod)) % mod)
  24. #define debug(x) cout << "x: " << x << nl;
  25. #define debug2(x, y) cout << "x: " << x << " y: " << y << nl;
  26. #define ordered_set tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  27. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  28.  
  29. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  30. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  31.  
  32. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  33.     for (auto& [x, y] : v) in >> x >> y;
  34.     return in;
  35. }
  36.  
  37. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  38.     for (T& i : v) in >> i;
  39.     return in;
  40. }
  41.  
  42. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  43.     for (const T& x : v)
  44.         out << x << ' ';
  45.     return out;
  46. }
  47.  
  48. void Start_Crushing() {
  49.     ios_base::sync_with_stdio(false);
  50.     cin.tie(nullptr);
  51.     cout.tie(nullptr);
  52. #ifndef ONLINE_JUDGE
  53.     freopen("input.txt", "r", stdin);
  54.     freopen("output.txt", "w", stdout);
  55. #endif
  56. }
  57. int n, w;
  58. vector<int> a, b;
  59.  
  60. vector<vector<int>> dp;
  61.  
  62. int knapsack(int idx = 0, int curr = 0){
  63.     if(idx == n - 1){
  64.         return b[idx] * (curr + a[idx] <= w);
  65.     }
  66.     int& res = dp[idx][curr];
  67.     if(~res)
  68.         return res;
  69.  
  70.     res = knapsack(idx + 1, curr);
  71.     if(curr + a[idx] <= w)
  72.         res = max(res, b[idx] + knapsack(idx + 1, curr + a[idx]));
  73.  
  74.     return res;
  75. }
  76.  
  77. void solve(){
  78.     cin >> n >> w;
  79.     a.resize(n), b.resize(n);
  80.     dp.assign(n + 1, vector<int>(w + 1, -1));
  81.  
  82.     for(int i = 0; i < n; i++)
  83.         cin >> a[i] >> b[i];
  84.  
  85.     cout << knapsack();
  86. }
  87.  
  88. int main(){
  89.     Start_Crushing();
  90.  
  91.     int t = 1;
  92. //    /*Multiple test cases?*/ cin >> t;
  93.     while (t--) {
  94.         solve();
  95.         if(!t) break;
  96.         cout << nl;
  97.     }
  98.  
  99. //    for(int tc = 1; tc <= t; tc++){
  100. //        cout << "Case #" << tc << ": ";
  101. //        solve();
  102. //        if(tc != t)
  103. //            cout << nl;
  104. //    }
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement