Saleh127

Untitled

May 11th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,a[100005],c,d,ma,i,j,k,l,ans,sum;
  6. while(cin>>n)
  7. {
  8. cin>>c;
  9. for(i=0;i<c;i++)
  10. {
  11. cin>>a[i];
  12. }
  13. ma=0;
  14. for(i=1;i<(1<<c);i++)
  15. {
  16. sum=0;
  17. for(j=0;j<c;j++)
  18. {
  19. if(i & (1<<j))
  20. {
  21. sum+=a[j];
  22. }
  23. if(sum<=n && sum>ma)
  24. {
  25. ma=sum;
  26. ans=i;
  27. }
  28. }
  29. }
  30. for(k=0;k<c;k++)
  31. {
  32. if(ans & (1<<k))
  33. {
  34. printf("%d ",a[k]);
  35. }
  36. }
  37. printf("sum:%d\n",ma);
  38. }
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment