Advertisement
matheus_monteiro

Wedding shopping

Jun 9th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int M, C;
  5. int price[30][30];
  6. int dp[30][300];
  7. int sz[30];
  8.  
  9. /* dp[i][j] = 1, se for possivel pegar exatamente 1 item de cada linha
  10.                  considerando até a linha atual de tal forma que resta
  11.                  j unidades de dinheiro
  12.              
  13.               0, caso contrario
  14. */
  15.  
  16. int main() {
  17.  
  18.     int t;
  19.     cin >> t;
  20.     while(t--) {   
  21.         cin >> M >> C;
  22.  
  23.         for(int i = 0; i < C; i++) {
  24.             cin >> sz[i];
  25.             for(int j = 0; j < sz[i]; j++)
  26.                 cin >> price[i][j];
  27.         }
  28.  
  29.         memset(dp, 0, sizeof(dp));
  30.  
  31.         for(int i = 0; i < sz[0]; i++)
  32.             if(M - price[0][i] >= 0)
  33.                 dp[0][ M - price[0][i] ] = 1;
  34.  
  35.         for(int i = 1; i < C; i++)
  36.             for(int money = 0; money <= M; money++)
  37.                 if(dp[i - 1][money])
  38.                     for(int k = 0; k < sz[i]; k++)
  39.                         if(money - price[i][k] >= 0)
  40.                             dp[i][ money - price[i][k] ] = 1;
  41.                        
  42.         int ans = -1;      
  43.         for(int money = 0; money <= M; money++)
  44.             if(dp[C - 1][money])
  45.                 ans = max(ans, M - money);
  46.            
  47.         if(ans == -1) cout << "no solution\n";
  48.         else cout << ans << '\n';
  49.     }
  50.  
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement