Beingamanforever

Buns, Knapsack Problem

Dec 8th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-08 18:22:31
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     // TODO: Fractional Knapsack variation, Greedy
  28.     int n, m, c0, d0;
  29.     cin >> n >> m >> c0 >> d0;
  30.     vi a(m + 1), b(m + 1), c(m + 1), d(m + 1);
  31.     for (int i = 1; i <= m; i++)
  32.     {
  33.         cin >> a[i] >> b[i] >> c[i] >> d[i];
  34.     }
  35.     // dp state -> using first m items, n grams dough -> maximum profit
  36.     // dp[i][j]
  37.     vvi dp(m + 1, vi(n + 1, 0));
  38.     for (int i = 1; i <= m; i++)
  39.     {
  40.         int maxtake = a[i] / b[i];   // maximum we can take of this type
  41.         for (int j = 0; j <= n; j++) //  grams of dough used
  42.         {
  43.             for (int k = 0; k <= maxtake; k++) // number of items we take of this type
  44.             {
  45.                 if (j + k * c[i] <= n)
  46.                 {
  47.                     dp[i][j] = max(dp[i][j], dp[i - 1][j + k * c[i]] + k * d[i]);
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     // now fill in the left ones with normal without stuffing
  53.     int ans = 0;
  54.     for (int i = 0; i <= n; i++)
  55.     {
  56.         int more = (i / c0) * d0;
  57.         dp[m][i] += more; // dp[m][i]-> maximum profit using all m items and i grams of dough
  58.         ans = max(ans, dp[m][i]);
  59.     }
  60.     cout << ans << endl;
  61.     return;
  62. }
  63.  
  64. signed main()
  65. {
  66.     NeedForSpeed;
  67.     int t = 1;
  68.     // cin >> t;
  69.     while (t--)
  70.     {
  71.         solve();
  72.     }
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment