Advertisement
Farjana_akter

Untitled

Feb 13th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int money,dress,dp[300][300],arr[50][50];
  4.  
  5. int wedding(int group,int money)
  6. {
  7. if(group==dress)
  8. return 0;
  9. if(dp[group][money]!=-1)
  10. return dp[group][money];
  11. int mx=-1000;
  12. for(int i=1;i<=arr[group][0];i++)
  13. {
  14. if(money-arr[group][i]>=0)
  15. mx=max(mx, wedding(group+1,money-arr[group][i])+arr[group][i]);
  16. }
  17. return dp[group][money]=mx;
  18. }
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24. int i,j,k,n,t;
  25. cin>>t;
  26. while(t--)
  27. {
  28. memset(dp,-1,sizeof(dp));
  29. cin>>money>>dress;
  30. for(i=0;i<dress;i++)
  31. {
  32. cin>>k;
  33. arr[i][0]=k;
  34. for(j=1;j<=arr[i][0];j++)
  35. {
  36. cin>>arr[i][j];
  37. }
  38. }
  39. int ans=wedding(0,money);
  40. if(ans>=0)
  41. cout<<ans<<endl;
  42. else
  43. cout<<"no solution"<<endl;
  44. }
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement