Advertisement
Josif_tepe

Untitled

Mar 18th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int niza[105];
  7.  
  8. int main()
  9. {
  10.     cin >> n;
  11.     for(int i = 0; i < n; i++) {
  12.         cin >> niza[i];
  13.     }
  14.     int x;
  15.     cin >> x;
  16.     int memo[x + 1];
  17.     for(int i = 0; i <= x; i++) {
  18.         memo[i] = 2000000;
  19.     }
  20.     memo[0] = 0;
  21.     for(int sum = 0; sum <= x; sum++) {
  22.         for(int i = 0; i < n; i++) {
  23.             if(sum + niza[i] <= x) {
  24.                 memo[sum + niza[i]] = min(memo[sum + niza[i]], memo[sum] + 1);
  25.             }
  26.         }
  27.     }
  28.     cout << memo[x] << endl;
  29.     return 0;
  30. }
  31. // sum = 0; i = 0
  32. // memo[1] = min(memo[1], memo[0] + 1) = 1
  33. // memo[2] = min(memo[2], memo[0] + 1) = 1
  34. // memo[5] = min(memo[5], memo[0] + 1) = 1
  35. // memo[7] = min(memo[7], memo[0] + 1) = 1
  36. // sum = 1
  37. // memo[2] = min(memo[2], memo[1] + 1) = 1
  38. // memo[3] = min(memo[3], memo[1] + 1) = 2
  39. // memo[6] = min(memo[6], memo[1] + 1) = 2
  40. // memo[8] = min(memo[8], memo[1] + 1) = 2
  41. // sum = 2
  42. // memo[3] = min(memo[3], memo[2] + 1) = 2
  43. // memo[4] = min(memo[4], memo[2] + 1) = 2
  44. // memo[7] = min(memo[7], memo[2] + 1) = 1
  45. // memo[9] = min(memo[9], memo[2] + 1) = 2
  46. // sum = 3
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement