xdu1179_better.cc 843 B

123456789101112131415161718192021222324252627282930313233
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. uint8_t f[100*100000+1];
  4. int val[5000];
  5. int main()
  6. {
  7. int n, s;
  8. while (scanf("%d%d", &n, &s)==2) {
  9. memset(f, 0x7f, sizeof(f));
  10. f[0] = 0;
  11. for (int i = 0; i<n; i++) {
  12. int v; scanf("%d", &v);
  13. val[i] = v;
  14. for (int j = 1; j<=s; j++)
  15. f[j*v] = min(f[j*v], (uint8_t)j);
  16. }
  17. int q; scanf("%d", &q);
  18. for (int i = 0; i<q; i++) {
  19. int ans = 101;
  20. int m; scanf("%d", &m);
  21. for (int j = 0; j<n; j++)
  22. for (int k = 0; k<=(s+1)/2; k++) {
  23. if (m<val[j]*k) break;
  24. ans = min(ans, k+f[m-val[j]*k]);
  25. }
  26. if (ans>s) puts("-1");
  27. else printf("%d\n", ans);
  28. }
  29. }
  30. return 0;
  31. }