AhmedAshraff

Untitled

Aug 1st, 2025
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3. #define Lemme_55555555555555555 ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  4.  
  5. #define ll  long long
  6. #define mem(arr, val) memset(arr,val,sizeof(arr))
  7. #define sz(s) (int) (s.size())
  8. #define all(vect) vect.begin(), vect.end()
  9. //freopen("Input.txt", "r", stdin); freopen("Output.txt", "w", stdout);
  10. int n,arr[305][305],B,W;
  11. ll dp[305][305],vid,vis[305][305];
  12. ll rec(int i,int take) {
  13.     if (i == n + 1) {
  14.         if (take == B)return 0;
  15.         return -1e16;
  16.     }
  17.     ll &ret = dp[i][take];
  18.     if (vis[i][take] == vid)return ret;
  19.     vis[i][take] = vid;
  20.     int posw = i - take;
  21.     ret = rec(i + 1, take) + arr[i][posw];
  22.     if (take < B)
  23.         ret = max(ret, rec(i + 1, take + 1) + arr[i][W + take + 1]);
  24.     return ret;
  25. }
  26. int main() {
  27.     Lemme_55555555555555555;
  28.     int t;
  29.     cin >> t;
  30.     while (t--) {
  31.         cin >> n;
  32.         for (int g = 1; g <= n; g++)
  33.             for (int i = 1; i <= n; i++)cin >> arr[g][i];
  34.  
  35.         ll mx = LLONG_MIN;
  36.         W = n, B = 0;
  37.         while (B < n) {
  38.             vid++;
  39.             mx = max(mx, rec(1, 0));
  40.             W--, B++;
  41.         }
  42.         cout << mx << "\n";
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment