Advertisement
erfanul007

UVa 624

Dec 4th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5. long long int N,n,i,j,k,l,a[30],t,max;
  6. while(scanf("%lld %lld",&N,&n)!=EOF){
  7. for(i=0;i<n;i++){
  8. scanf("%lld",&a[i]);
  9. }
  10. k=pow(2,n);
  11. max=0;
  12. for(i=0;i<k;i++){
  13. t=0;
  14. for(j=0;j<n;j++){
  15. if(i&(1<<j))
  16. t=t+a[j];
  17. }
  18. if(t>max && t<=N){
  19. max=t;
  20. l=i;
  21. }
  22. }
  23. for(j=0;j<n;j++){
  24. if(l&(1<<j))
  25. printf("%lld ",a[j]);
  26. }
  27. printf("sum:%lld\n",max);
  28. }
  29. return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement