Advertisement
Josif_tepe

Untitled

Jun 1st, 2025
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long ll;
  5. int n,W;
  6. int x[100][2];
  7. ll dp[101][100001];
  8. const ll infinity=1e13;
  9. ll rec(int at,int w){
  10.   if(w==0){
  11.     return 0;
  12.   }
  13.   if(at>=n){
  14.       return 0;
  15.   }
  16.     if(dp[at][w] != -1) {
  17.         return dp[at][w];
  18.     }
  19.   ll rez=-infinity;
  20.   rez=max(rez,rec(at+1,w));
  21.   if(w>=x[at][0]){
  22.     rez=max(rez,rec(at+1,w-x[at][0])+x[at][1]);
  23.   }
  24.     dp[at][w] = rez;
  25.   return rez;
  26. }
  27.  
  28. int main()
  29. {
  30.     memset(dp, -1, sizeof dp);
  31.     cin>>n>>W;
  32.     for(int i = 0;i<n;i++){
  33.         cin>>x[i][0];
  34.         cin>>x[i][1];
  35.     }
  36.     cout<<rec(0, W);
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement