Advertisement
Farjana_akter

Untitled

Jun 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[10000],n,track,mx,k=0;
  4. vector<int>res,saiful;
  5. void cd(int depth,int sum)
  6. {
  7. if(sum>n)
  8. {
  9. return;
  10. }
  11. if(depth==track)
  12. {
  13. if(sum>mx)
  14. {
  15. res=saiful;
  16. mx=sum;
  17. cout<<"res vector"<<endl;
  18. for(int i=0; i<res.size(); i++)
  19. cout<<res[i]<<" ";
  20. cout<<endl;
  21. cout<<"Max value is: "<<mx<<endl;
  22. }
  23. return;
  24. }
  25. cout<<"sum is: "<<sum<<endl;
  26. cout<<endl;
  27. saiful.push_back(a[depth]);
  28. cout<<"saiful after push back"<<endl;
  29. for(int i=0; i<saiful.size(); i++)
  30. cout<<saiful[i]<<" ";
  31. cout<<endl;
  32. cd(depth+1,sum+a[depth]);
  33. saiful.pop_back();
  34. cout<<"saiful after pop back"<<endl;
  35. for(int i=0; i<saiful.size(); i++)
  36. cout<<saiful[i]<<" ";
  37. cout<<endl;
  38. cd(depth+1,sum);
  39.  
  40. }
  41. int main()
  42. {
  43. int i,j;
  44. while(cin>>n>>track)
  45. {
  46. saiful.clear();
  47. res.clear();
  48. for(i=0; i<track; i++)
  49. {
  50. cin>>a[i];
  51. }
  52. mx=0;
  53. cd(0,0);
  54. int jog=0;
  55. for(i=0; i<res.size(); i++)
  56. {
  57. cout<<res[i]<<' ';
  58. jog+=res[i];
  59. }
  60. cout<<"sum:"<<jog<<endl;
  61. }
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement