Advertisement
Josif_tepe

Untitled

Sep 25th, 2022
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. //#include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11.     ios_base::sync_with_stdio(true);
  12.     int n,W;
  13.     cin>>n>>W;
  14.     long long dp[n+1][W+1];
  15.     for(int i=0;i<=n;i++)
  16.     {
  17.         for(int j=0;j<=W;j++)
  18.         {
  19.             dp[i][j]=0;
  20.         }
  21.     }
  22.     int x[n][2];
  23.     for(int i=0;i<n;i++)
  24.     {
  25.         for(int j=1;j>=0;j--)cin>>x[i][j];
  26.     }
  27.     for(int i = 0; i <= n; i++) {
  28.         for(int j = 0; j <= W; j++) {
  29.             dp[i][j] = -1e18;
  30.         }
  31.     }
  32.     dp[0][W] = 0;
  33.     for(int i=0;i<n;i++)
  34.     {
  35.         for(int w=W;w>=0;w--)
  36.         {
  37.             dp[i + 1][w] = max(dp[i + 1][w], dp[i][w]);
  38.             if(w - x[i][1] >= 0) {
  39.                 dp[i + 1][w - x[i][1]] = max(dp[i + 1][w - x[i][1]], dp[i][w] + x[i][0]);
  40.             }
  41.         }
  42.     }
  43.     long long ans=0;
  44.     for(int j=0;j<=W;j++)
  45.     {
  46.         ans=max(dp[n][j],ans);
  47.     }
  48.     cout<<ans<<endl;
  49.     return 0;
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement