Mirbek

Minimizing Coins

Dec 22nd, 2021
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1e6 + 3;
  6.  
  7. int n, x;
  8. int dp[N], a[N];
  9.  
  10. int main(){
  11.     cin >> n >> x;
  12.  
  13.     for (int i = 1; i <= n; i++) {
  14.         cin >> a[i];
  15.     }
  16.  
  17.     for (int i = 1; i <= x; i++) {
  18.         dp[i] = 1e7;
  19.     }
  20.  
  21.     for (int i = 1; i <= n; i++) {
  22.         for (int j = a[i]; j <= x; j++) {
  23.             dp[j] = min(dp[j], dp[j - a[i]] + 1);
  24.         }
  25.     }
  26.  
  27.     if (dp[x] == 1e7) dp[x] = -1;
  28.     cout << dp[x] << endl;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment