Advertisement
Josif_tepe

Untitled

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