Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int func(int i,int w,int *cost,int *weight,int m,int n);
  5.  
  6. int profit1,profit2;
  7.  
  8. int main()
  9. {
  10. int a,t,n,p,m;
  11. cin>>t;
  12. while(t--)
  13. {
  14. cin>>n;
  15. int cost[n],weight[n];
  16. for(a=0;a<n;a++)
  17. {
  18. cin>>cost[a]>>weight[a];
  19. }
  20. cin>>p;
  21. while(p--)
  22. {
  23. cin>>m;
  24. int k=func(0,0,cost,weight,m,n);
  25. cout<<k<<endl;
  26. }
  27. }
  28. return 0;
  29. }
  30.  
  31. int func(int i,int w,int *cost,int *weight,int m,int n)
  32. {
  33. //cout<<profit1<<" "<<profit2<<endl;;
  34. if(i=n-1) return 0;
  35. else
  36. {
  37. if(w+weight[i]<=m) profit1=cost[i]+func(i+1,w+weight[i],cost,weight,m,n);
  38. else profit1=0;
  39. profit2=func(i+1,w,cost,weight,m,n);
  40. return max(profit1,profit2);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement