Advertisement
sherry_ahmos

Untitled

Dec 1st, 2022
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. #define ll long long
  9. #define cy cout << "YES"
  10. #define cn cout << "NO"
  11. #define nl "\n"
  12. #define fi first
  13. #define se second
  14. #define all(v) v.begin(), v.end()
  15. #define sz(s) s.size()
  16. template <typename T = int>
  17. istream &operator>>(istream &in, vector<T> &v)
  18. {
  19.     for (auto &x : v)
  20.         in >> x;
  21.     return in;
  22. }
  23.  
  24. template <typename T = int>
  25. ostream &operator<<(ostream &out, const vector<T> &v)
  26. {
  27.     for (const T &x : v)
  28.         out << x << " ";
  29.     return out;
  30. }
  31.  
  32. void sherry()
  33. {
  34.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  35. #ifndef ONLINE_JUDGE
  36.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  37. #endif
  38. }
  39. int n, x;
  40. vector<int> v, dp;
  41. int coins(int curr)
  42. {
  43.     if (curr == 0)
  44.         return 0;
  45.     int &ret = dp[curr];
  46.     if (~ret)
  47.         return ret;
  48.     ret = 1e6;
  49.     for (int i = 0; i < n; i++)
  50.     {
  51.         if (curr - v[i] >= 0)
  52.         {
  53.             ret = min(ret, 1 + coins(curr - v[i]));
  54.         }
  55.     }
  56.     return ret;
  57. }
  58. void solve()
  59. {
  60.     cin >> n >> x;
  61.     v.resize(n);
  62.     cin >> v;
  63.     dp.assign(1e6, -1);
  64.     int ans = coins(x);
  65.     if (ans == 1e6) cout << "-1" ;
  66.     else cout << ans;
  67. }
  68. int main()
  69. {
  70.     sherry();
  71.     int t = 1;
  72.     // cin >> t;
  73.     while (t--)
  74.         solve();
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement