Advertisement
Josif_tepe

Untitled

Nov 3rd, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include<ctime>
  3. #include<map>
  4. #include<vector>
  5. #include<queue>
  6. using namespace std;
  7. typedef long long ll;
  8. int n, W;
  9. int weights[105], values[105];
  10. ll dp[105][100005];
  11. ll rec(int i, int value_left) {
  12.     if(value_left == 0) {
  13.         return 0;
  14.     }
  15.     if(i == n) {
  16.         return 1e18;
  17.     }
  18.     if(dp[i][value_left] != -1) {
  19.         return dp[i][value_left];
  20.     }
  21.     ll result = 1e18; // 1 i 18 nuli posle edinicata 1000...0
  22.     result = min(result, rec(i + 1, value_left));
  23.     if(value_left - values[i] >= 0) {
  24.         result = min(result, rec(i + 1, value_left - values[i]) + weights[i]);
  25.     }
  26.     dp[i][value_left] = result;
  27.     return result;
  28. }
  29. int main()
  30. {
  31.     cin >> n >> W;
  32.     int sum = 0;
  33.     for(int i = 0; i < n; i++) {
  34.         cin >> weights[i] >> values[i];
  35.         sum += values[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.     for(int i = sum; i >= 0; i--) {
  43.         if(rec(0, i) <= W) {
  44.             cout << i << endl;
  45.             break;
  46.         }
  47.     }
  48.    
  49.     return 0;
  50. }
  51.  
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement