giabinh

SanXuat

Sep 28th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, m, t[1003][1003];
  6.  
  7. int SanXuat(int i, int c) {
  8.     if (c < 0) return(-1);
  9.     if (i > n) return(m - c);
  10.     int res = -1;
  11.     for (int j=1; j<=t[i][0]; ++j)
  12.         res = max(res, SanXuat(i+1, c-t[i][j]));
  13.     return(res);
  14. }
  15.  
  16. int main() {
  17.     freopen("SanXuat.inp", "r", stdin);
  18.  
  19.     int test; scanf("%d", &test);
  20.     while (test--)
  21.     {
  22.         scanf("%d%d", &m, &n);
  23.         for (int i=1; i<=n; ++i)
  24.         {
  25.             scanf("%d", &t[i][0]);
  26.             for (int j=1; j<=t[i][0]; ++j) scanf("%d", &t[i][j]);
  27.         }
  28.  
  29.         printf("%d\n", SanXuat(1, m));
  30.     }
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment