shimulxx

Untitled

Apr 12th, 2021
637
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const int INF = 1e9;
  6.  
  7. ll n, x;
  8. ll coin[105];
  9. ll dp[1000005];
  10. set<ll> s;
  11.  
  12. ll solve(int i, ll sum)
  13. {
  14.     if (sum == x)
  15.         return 0;
  16.  
  17.     if (i == n)
  18.         return INF;
  19.  
  20.     if (sum>x)
  21.         return INF;
  22.  
  23.     if (dp[sum] != -1)
  24.         return dp[sum];
  25.  
  26.     ll r1, r2;
  27.     r1 = solve(i, sum + coin[i]) + 1;
  28.     r2 = solve(i + 1, sum);
  29.  
  30.     return dp[sum] = min(r1, r2);
  31.  
  32. }
  33.  
  34. int main()
  35. {
  36.     //freopen("input.txt","r",stdin);
  37.     //freopen("output.txt","w",stdout);*/
  38.  
  39.     cin >> n >> x;
  40.  
  41.     for (int i = 0; i < n; i++)
  42.     {
  43.         ll val; cin >> val;
  44.         s.insert(val);
  45.     }
  46.     n = s.size();
  47.     int i = 0;
  48.     for (auto it = s.begin(); it != s.end(); ++it, ++i) {
  49.         coin[i] = *it;
  50.     }
  51.     memset(dp, -1, sizeof(dp));
  52.  
  53.     ll ans = solve(0, 0);
  54.  
  55.     if (ans == INF)
  56.         cout << "-1" << endl;
  57.     else
  58.         cout << ans << endl;
  59.     return 0;
  60. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×