Saleh127

UVA 11450

May 3rd, 2021 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(0);
  8. cin.tie(0);
  9. cout.tie(0);
  10.  
  11. test
  12. {
  13.  
  14. ll n,m,i,j,k,l=0,c;
  15.  
  16. cin>>n>>c;
  17.  
  18. ll dp[c+5][n+2];
  19.  
  20. memset(dp,0,sizeof dp);
  21.  
  22. vector<ll>a[c+5];
  23.  
  24. for(i=1; i<=c; i++)
  25. {
  26. cin>>j;
  27. while(j--)
  28. {
  29. cin>>k;
  30. a[i].push_back(k);
  31. }
  32. }
  33.  
  34. dp[0][0]=1;
  35.  
  36. for(i=1;i<=c;i++)
  37. {
  38. for(j=n;j>=0;j--)
  39. {
  40. for(k=a[i].size()-1;k>=0;k--)
  41. {
  42. if(a[i][k]<=j && dp[i][j]<dp[i-1][j-a[i][k]])
  43. {
  44. //dp[i][j]<dp[i-1][j-a[i][k]] ei check ta na dileo hobe
  45. dp[i][j]+=dp[i-1][j-a[i][k]];
  46. }
  47. }
  48. }
  49. }
  50.  
  51. for(i=0;i<=n;i++)
  52. {
  53. if(dp[c][i]>0)
  54. {
  55. l=max(l,i);
  56. }
  57. }
  58.  
  59. if(l) cout<<l<<endl;
  60. else cout<<"no solution"<<endl;
  61.  
  62. }
  63.  
  64.  
  65. return 0;
  66. }
  67.  
Add Comment
Please, Sign In to add comment