Advertisement
a53

Rucsac_Halloween

a53
Mar 11th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int n, a[1000], v[10001], G;
  6.  
  7. bool cmp(int x, int y)
  8. {
  9. return x < y;
  10. }
  11.  
  12. int main()
  13. {
  14. cin>>n>>G;
  15. for(int i = 0; i < n; i++)
  16. cin>>a[i];
  17.  
  18. sort(a, a + n, cmp);
  19.  
  20. for(int i = 0; i < n; i++)
  21. {
  22. int s = 0;
  23. v[a[i]] = 1;
  24. for(int j = 1; j <= G; j++)
  25. {
  26. if(v[j] && j != a[i])
  27. {
  28. s = j + a[i];
  29. if(s <= G)
  30. {
  31. if(v[s] == 0)
  32. v[s] = v[j] + 1;
  33. else
  34. v[s] = min(v[s], v[j] + 1);
  35. }
  36. else
  37. break;
  38. }
  39. }
  40. }
  41. if(v[G])
  42. cout<<v[G];
  43. else
  44. cout<<"NU";
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement