Advertisement
Josif_tepe

Untitled

Nov 28th, 2023
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5. const int maxn = 25;
  6. int n, m, k;
  7. int lock_cost[maxn];
  8. int rating[maxn][maxn];
  9. int cost[maxn][maxn];
  10. int dp[maxn][1005][2];
  11. int rec(int at_batch, int money_left, bool is_locked) {
  12.     if(at_batch >= n) {
  13.         return 0;
  14.     }
  15.     if(dp[at_batch][money_left][is_locked] != -1) {
  16.         return dp[at_batch][money_left][is_locked];
  17.     }
  18.     int real_money = money_left;
  19.     if(is_locked) {
  20.         real_money -= lock_cost[at_batch];
  21.     }
  22.     int res = 0;
  23.     if(real_money >= 0) {
  24.         for(int i = 0; i < m; i++) {
  25.             if(real_money >= cost[at_batch][i]) {
  26.                 res = max(res, rec(at_batch, real_money - cost[at_batch][i], false) + rating[at_batch][i]);
  27.                
  28.                 res = max(res, rec(at_batch + 1, real_money - cost[at_batch][i], true) + rating[at_batch][i]);
  29.             }
  30.         }
  31.     }
  32.     res = max(res, rec(at_batch + 1, money_left, true));
  33.     return dp[at_batch][money_left][is_locked] = res;
  34. }
  35. int main()
  36. {
  37.     ios_base::sync_with_stdio(false);
  38.     int t;
  39.     cin >> t;
  40.    
  41.     while(t--) {
  42.         cin >> n >> m >> k;
  43.         for(int i = 0; i < n; i++) {
  44.             cin >> lock_cost[i];
  45.         }
  46.         for(int i = 0; i < n; i++) {
  47.             for(int j = 0; j < m; j++) {
  48.                 cin >> cost[i][j];
  49.             }
  50.         }
  51.        
  52.         for(int i = 0; i < n; i++) {
  53.             for(int j = 0; j < m; j++) {
  54.                 cin >> rating[i][j];
  55.             }
  56.         }
  57.         for(int i = 0; i < maxn; i++) {
  58.             for(int j = 0; j < 1004; j++) {
  59.                 for(int k = 0; k < 2; k++) {
  60.                     dp[i][j][k] = -1;
  61.                 }
  62.             }
  63.         }
  64.         cout << rec(0, k, true) << endl;
  65.        
  66.     }
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement