Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7.  
  8.  
  9.  
  10.  
  11. int main() {
  12. ios_base::sync_with_stdio(false);
  13. cin.tie(NULL);
  14. cout.tie(NULL);
  15.  
  16. vector <int> a,b(2001);
  17. int n, k,q(-1),min(1e9);
  18. cin >> n;
  19. a.resize(n);
  20.  
  21. for (int i(0); i < n; i++)
  22. {
  23. cin >> a[i];
  24. b[a[i]] = 1;
  25. }
  26. cin >> k;
  27. b.resize(k+1);
  28. sort(a.begin(), a.end());
  29. for (int i(1); i <= k; i++)
  30. {
  31. if (b[i] != 1){
  32. for (int j(0); j < n; j++)
  33. if (a[j] < i)
  34. q = j;
  35. for (int j(0); j <= q; j++)
  36. if (b[i - a[j]] < min)
  37. min = b[i - a[j]];
  38.  
  39. b[i] = min + 1;
  40.  
  41. min = 1e9;
  42.  
  43. }
  44. }
  45. if (b[k] == 1e9+1)
  46. cout << -1;
  47. else
  48. cout << b[k];
  49.  
  50. return 0;
  51. }
  52.  
  53.  
  54. /*
  55.  
  56.  
  57. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement