fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=1e9+7;
  5. const ll INF = 10000000000000;
  6. const int N = 1e6+1;
  7.  
  8. void solve() {
  9. ll n,x;
  10. cin >> n >> x;
  11. vector<int> a(n+1);
  12. for(int i=1;i<=n;i++) cin >> a[i];
  13. vector<ll> dp(N,INF);
  14. for(int i=1;i<=n;i++) dp[a[i]]=1;
  15. for(int i=1;i<N;++i){
  16. for(int j=1;j<=n;j++){
  17. if(dp[i]!=INF){
  18. dp[i+a[j]]=min(dp[i+a[j]],dp[i]+1);
  19. }
  20. }
  21. }
  22. if(dp[x]==INF) cout << -1;
  23. else cout << dp[x];
  24. }
  25.  
  26. int main(){
  27. ios::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29.  
  30. /*int t;
  31.   cin >> t;
  32.   while (t--)*/ solve();
  33.  
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 11168KB
stdin
3 11
1 5 7
stdout
3