Advertisement
Josif_tepe

Untitled

Jan 30th, 2022
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <fstream>
  5. #include <array>
  6. using namespace std;
  7. typedef long long ll;
  8. vector<int> palindromes;
  9. int n, weight_max;
  10. int w[105], v[105];
  11. ll dp[105][100005];
  12.  
  13. int main()
  14. {
  15.     cin >> n >> weight_max;
  16.     int sum = 0;
  17.     for(int i = 0; i < n; i++) {
  18.         cin >> w[i] >> v[i];
  19.         sum += v[i];
  20.     }
  21.     for(int i = 0; i <= n; i++) {
  22.         for(int j = 0; j <= sum; j++) {
  23.             dp[i][j] = 2e9;
  24.         }
  25.     }
  26.     dp[0][0] = 0;
  27.     for(int i = 0; i < n; i++) {
  28.         for(int j = 0; j <= sum; j++) {
  29.             dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
  30.             if(j + v[i] <= sum) {
  31.                 dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
  32.             }
  33.         }
  34.     }
  35.    
  36.     for(int i = sum; i >= 0; i--) {
  37.         if(dp[n][i] <= weight_max) {
  38.             cout << i << endl;
  39.             return 0;
  40.         }
  41.     }
  42.     return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement