Advertisement
Josif_tepe

Untitled

Oct 20th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, x;
  6. int niza[105];
  7. int dp[1000003];
  8. int coins(int x_left) {
  9.     if(x_left == 0) {
  10.         return 0;
  11.     }
  12.     if(dp[x_left] != -1) {
  13.         return dp[x_left];
  14.     }
  15.     int result = 2e9;
  16.     for(int i = 0; i < n; i++) {
  17.         if(x_left - niza[i] >= 0) {
  18.             result = min(result, coins(x_left - niza[i]) + 1);
  19.         }
  20.     }
  21.     return dp[x_left] = result;
  22. }
  23. int main()
  24. {
  25.     cin >> n >> x;
  26.     for(int i = 0; i <= x; i++) {
  27.         dp[i] = -1;
  28.     }
  29.     for(int i = 0; i < n; i++) {
  30.         cin >> niza[i];
  31.     }
  32.     if(coins(x) == 2e9) {
  33.         cout << -1 << endl;
  34.     }
  35.    else  cout << coins(x) << endl;
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement