Advertisement
Hezov

NOI18 Knapsack 73p

Apr 4th, 2025
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. // https://oj.uz/problem/view/NOI18_knapsack 73 puncte
  2. #include <iostream>
  3. using namespace std;
  4. int dp[101][2001], v[2001], w[2001], k[2001];
  5. int main()
  6. {
  7.     int S, N;
  8.     cin >> S >> N;
  9.     for(int i = 1;i<=N;i++)
  10.         cin >> v[i] >> w[i] >> k[i];
  11.     for(int i = 0;i<=N;i++)
  12.     {
  13.         for(int j = 0;j<=S;j++)
  14.         {
  15.             if(i == 0 || j == 0) dp[i][j] = 0;
  16.             else
  17.             {
  18.                 dp[i][j] = dp[i-1][j];
  19.                 for(int nr = 1; nr * w[i] <= j && nr <= k[i];nr++)
  20.                 {
  21.                     dp[i][j] = max(dp[i][j], dp[i - 1][j - nr * w[i]] + v[i] * nr);
  22.                 }
  23.             }
  24.         }
  25.     }
  26.     cout << dp[N][S];
  27.     return 0;
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement