Advertisement
Josif_tepe

Untitled

Feb 3rd, 2024
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <cstring>
  5. #include <set>
  6. //#include<bits/stdc++.h>
  7. using namespace std;
  8. typedef long long ll;
  9.  
  10. const ll INF = 1e10;
  11. const int maxn = 2005;
  12. int s, n;
  13. int v[100005], w[100005], k[100005];
  14. vector<pair<int, int> > items[maxn];
  15. ll dp[maxn][maxn];
  16. ll rec(int at, int left) {
  17.     if(left < 0) {
  18.         return -INF;
  19.     }
  20.     if(at < 0) {
  21.         return 0;
  22.     }
  23.     if(dp[at][left] != -1) {
  24.         return dp[at][left];
  25.     }
  26.     ll res = rec(at - 1, left);
  27.     int current_weight = 0;
  28.     int sum_value = 0;
  29.     for(pair<int, int> p : items[at]) {
  30.         for(int i = 1; i <= p.second; i++) {
  31.             current_weight += at;
  32.             sum_value += p.first;
  33.             if(current_weight > left) {
  34.                 break;
  35.             }
  36.             res = max(res, rec(at - 1, left - current_weight) + sum_value);
  37.         }
  38.         if(current_weight > left) {
  39.             break;
  40.         }
  41.     }
  42.    
  43.     return dp[at][left] = res;
  44. }
  45.  
  46. int main() {
  47.     ios_base::sync_with_stdio(false);
  48.     cin >> s >> n;
  49.     for(int i = 0; i < maxn; i++) {
  50.         for(int j = 0; j < maxn; j++) {
  51.             dp[i][j] = -1;
  52.         }
  53.     }
  54.     int max_weight = 0;
  55.     for(int i = 0; i < n; i++) {
  56.         cin >> v[i] >> w[i] >> k[i];
  57.         items[w[i]].push_back(make_pair(v[i], k[i]));
  58.         max_weight = max(max_weight, w[i]);
  59.     }
  60.     for(int i = 0; i <= s; i++) {
  61.         sort(items[i].rbegin(), items[i].rend());
  62.     }
  63.     cout << rec(max_weight, s) << endl;
  64.     return 0;
  65. }
  66.  
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement