stillnotred

Untitled

Dec 8th, 2025
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #pragma GCC optimize("O3")
  3. using namespace std;
  4. #define int long long
  5. const int N = 1e6 + 10;
  6. const int inf = 1e15;
  7. const int mod = 998244353;
  8.  
  9. inline void solve(){
  10.  
  11. int n;cin >> n;
  12. vector<vector<int>> v(n, vector<int>(n));
  13.  
  14. for(auto &x : v) for(auto &y : x) cin >> y;
  15.  
  16.  
  17. int ans = 0;
  18.  
  19. // sum[i][mask] : sum of row i, and mask columns
  20. // for row i and i + 1, i need to check same column and adjacent columns with adjacent rows
  21. // but then i need to find the counter mask of this current mask which is easy and iterate on the
  22. // maximal subset of countermask which i dont like
  23. // masks are common, they dont care about there position so i can find the subset of masks
  24. // which are compatible with each other
  25. // again the number of valid masks change everything
  26.  
  27.  
  28.  
  29.  
  30. auto valid = [&](int mask){
  31. return (mask & (mask << 1)) == 0;
  32. };
  33.  
  34. vector<int> good;
  35.  
  36. for(int mask = 0;mask < (1 << n);mask += 1){
  37. if(valid(mask)){
  38. good.push_back(mask);
  39. }
  40. }
  41.  
  42. // cout << good.size() << ' ';
  43.  
  44. int m = good.size();
  45.  
  46. vector<vector<int>> g(m);
  47.  
  48. for(int i = 0;i < m;i += 1){
  49. for(int j = i + 1;j < m;j += 1){
  50. int up = good[i], down = good[j];
  51.  
  52. if((up & down) > 0){
  53. continue;
  54. }
  55.  
  56. if(((up << 1) & down) > 0 || ((up >> 1) & down) > 0){
  57. continue;
  58. }
  59.  
  60. g[i].push_back(j);
  61. g[j].push_back(i);
  62.  
  63. }
  64. }
  65.  
  66. vector<vector<int>> sum(n, vector<int>(m, -inf));
  67.  
  68. for(int i = 0;i < n;i += 1){
  69. for(int k = 0;k < m;k += 1){
  70. int cur = 0;
  71. int mask = good[k];
  72.  
  73. for(int j = 0;j < n;j += 1){
  74. if(mask >> j & 1){
  75. cur += v[i][j];
  76. }
  77. }
  78.  
  79. sum[i][k] = cur;
  80. }
  81. }
  82.  
  83. vector<int> dp(m), ndp(m);
  84.  
  85. for(int i = 0;i < m;i += 1){
  86. dp[i] = sum[0][i];
  87. }
  88.  
  89. for(int i = 1;i < n;i += 1){
  90.  
  91. ndp.assign(m, -inf);
  92.  
  93. for(int j = 0;j < m;j += 1){
  94. if(dp[j] == -inf) continue;
  95.  
  96. for(auto &x : g[j]){
  97. ndp[x] = max(ndp[x], dp[j] + sum[i][x]);
  98. }
  99. }
  100.  
  101. dp = ndp;
  102. }
  103.  
  104. cout << *max_element(dp.begin(), dp.end());
  105.  
  106. }
  107.  
  108. int32_t main(){
  109. ios_base::sync_with_stdio(false);
  110. cin.tie(nullptr);
  111.  
  112. int t = 1;
  113. cin >> t;
  114. while(t--){
  115. solve();
  116. cout << '\n';
  117. }
  118. return 0;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment