Advertisement
Josif_tepe

Untitled

Apr 13th, 2022
946
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int cena[2005], tezina[2005];
  7. int W;
  8.  
  9. int rec(int at, int W_left) {
  10.     if(at == n) {
  11.         return 0;
  12.     }
  13.    
  14.     int result = -2e9;
  15.     // da ne go zememe elementot
  16.     result = max(result, rec(at + 1, W_left));
  17.    
  18.     if(W_left - tezina[at] >= 0)
  19.         result = max(result, rec(at + 1, W_left - tezina[at]) + cena[at]);
  20.     return result;
  21. }
  22. int main() {
  23.     cin >> n;
  24.     for(int i = 0; i < n; i++) {
  25.         cin >> cena[i] >> tezina[i];
  26.     }
  27.     cin >> W;
  28.     cout << rec(0, W) << endl;
  29.     return 0;
  30. }
  31.  
Advertisement
RAW Paste Data Copied
Advertisement