Advertisement
Josif_tepe

Untitled

Jan 30th, 2022
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 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. ll knapsack(int at, int value) {
  13.     if(value == 0) {
  14.         return 0;
  15.     }
  16.     if(at == n) {
  17.         return 1e9;
  18.     }
  19.     if(dp[at][value] != -1) {
  20.         return dp[at][value];
  21.     }
  22.     ll result = 1e9;
  23.     result = min(result, knapsack(at + 1, value));
  24.     if(value - v[at] >= 0)
  25.         result = min(result, knapsack(at + 1, value - v[at]) + w[at]);
  26.    
  27.     return dp[at][value] = result;
  28. }
  29. int main()
  30. {
  31.     cin >> n >> weight_max;
  32.     int sum = 0;
  33.     for(int i = 0; i < n; i++) {
  34.         cin >> w[i] >> v[i];
  35.         sum += v[i];
  36.     }
  37.     for(int i = 0; i <= n; i++) {
  38.         for(int j = 0; j <= sum; j++) {
  39.             dp[i][j] = -1;
  40.         }
  41.     }
  42.    
  43.     for(int i = sum; i >= 0; i--) {
  44.         if(knapsack(0, i) <= weight_max) {
  45.             cout << i << endl;
  46.             break;
  47.         }
  48.     }
  49.     return 0;
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement