Advertisement
LA77

Untitled

Feb 25th, 2025
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ifstream fin("rucsac.in");
  5. ofstream fout("rucsac.out");
  6.  
  7. const int NMAX = 1e4+5;
  8. int n, g, w[NMAX], p[NMAX], dp[NMAX], ans = 0;
  9.  
  10. inline void solve()
  11. {
  12.     for(int i=1;i<=n;++i)
  13.     {
  14.         for(int j=g-w[i];j>=0;--j)
  15.         {
  16.             if(dp[j+w[i]] < dp[j] + p[i])
  17.             {
  18.                 dp[j+w[i]] = dp[j]+p[i];
  19.                 ans = max(dp[j+w[i]], ans);
  20.             }
  21.         }
  22.     }
  23.     fout << ans;
  24. }
  25.  
  26. int main()
  27. {
  28.     fin >> n >> g;
  29.     for(int i=1;i<=n;++i)
  30.         fin >> w[i] >> p[i];
  31.     solve();
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement