fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, a[10001], s, f[1000009];
  5.  
  6. int main(){
  7. cin >> n >> s;
  8. for(int i = 1; i <= n; ++i)
  9. cin >> a[i];
  10. f[0] = 0;
  11. for(int i = 1; i <= s; ++i){
  12. f[i] = INT_MAX;
  13. for(int j = 1; j <= n; ++j){
  14. int x = i - a[j];
  15. if(x == 0)
  16. f[i] = 1;
  17. else if(x > 0 && f[x] != 0){
  18. f[i] = min(f[x] + 1, f[i]);
  19. }
  20. }
  21. if(f[i] == INT_MAX)
  22. f[i] = 0;
  23. }
  24. if(f[s] == 0)
  25. cout << -1;
  26. else
  27. cout << f[s];
  28. return 0;
  29. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
-1