Advertisement
matheus_monteiro

Wedding shopping - Bitset

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