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