Advertisement
Josif_tepe

Untitled

Mar 3rd, 2022
1,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. #define ll long long
  7. #define pb push_back
  8. #define mp make_pair
  9.  
  10. int n, c;
  11. vector<pair<int, int>> weight;
  12. int dp[100][30000];
  13.  
  14. int rec(int idx, int w){
  15.     if(idx == c) return 0;
  16.  
  17.     if(dp[idx][w] != -1){
  18.         return dp[idx][w];
  19.     }
  20.  
  21.     int result = -2e9;
  22.  
  23.     result = max(result, rec(idx + 1, w));
  24.     if(weight[idx].second <= w){
  25.         result = max(result, rec(idx + 1, w - weight[idx].second + weight[idx].first) + 1);
  26.     }
  27.     dp[idx][w] = result;
  28.     return result;
  29. }
  30.  
  31.  
  32. int main(){
  33.  
  34.     ios_base::sync_with_stdio(false);
  35.     cout.tie(0);
  36.     cin.tie(0);
  37.  
  38.  
  39.     cin >> n >> c;
  40.     for(int i = 0; i < c; i++){
  41.         int a, b;
  42.         cin >> a >> b;
  43.         weight.pb(mp(b, a));
  44.     }
  45.  
  46.     sort(weight.rbegin(), weight.rend());
  47.  
  48.     for(int i = 0; i < c; i++){
  49.         for(int j = 0; j < 30000; j++){
  50.             dp[i][j] = -1;
  51.         }
  52.     }
  53.  
  54.     cout << rec(0, n) << endl;
  55.  
  56.     return 0;
  57. }
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement