fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int n,x;
  7. cin>>n>>x;
  8.  
  9. int a[n];
  10. for(int i=0;i<n;i++)
  11. {
  12. cin>>a[i];
  13. }
  14.  
  15. pair<int,int> dp[1<<n];
  16. dp[0] = {0,x+1};
  17.  
  18. for(int s=1;s<(1<<n);s++)
  19. {
  20. dp[s] = {25,0};
  21. for(int i=0;i<n;i++)
  22. {
  23. if((s>>i)&1)
  24. {
  25. auto [c,w] = dp[s^(1<<i)];
  26. if(w + a[i] > x)
  27. {
  28. c++;
  29. w = min(a[i],w);
  30. }
  31. else
  32. {
  33. w+=a[i];
  34. }
  35. dp[s] = min(dp[s],{c,w});
  36. }
  37. }
  38.  
  39. }
  40. cout<<dp[(1<<n)-1].first<<endl;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5280KB
stdin
4 10
4 8 6 1
stdout
2