<p>You are given a <code>m x n</code> 2D array <code>board</code> representing a chessboard, where <code>board[i][j]</code> represents the <strong>value</strong> of the cell <code>(i, j)</code>.</p>

<p>Rooks in the <strong>same</strong> row or column <strong>attack</strong> each other. You need to place <em>three</em> rooks on the chessboard such that the rooks <strong>do not</strong> <strong>attack</strong> each other.</p>

<p>Return the <strong>maximum</strong> sum of the cell <strong>values</strong> on which the rooks are placed.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = </span>[[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]</p>

<p><strong>Output:</strong> 4</p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/08/rooks2.png" style="width: 294px; height: 450px;" /></p>

<p>We can place the rooks in the cells <code>(0, 2)</code>, <code>(1, 3)</code>, and <code>(2, 1)</code> for a sum of <code>1 + 1 + 2 = 4</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[1,2,3],[4,5,6],[7,8,9]]</span></p>

<p><strong>Output:</strong> <span class="example-io">15</span></p>

<p><strong>Explanation:</strong></p>

<p>We can place the rooks in the cells <code>(0, 0)</code>, <code>(1, 1)</code>, and <code>(2, 2)</code> for a sum of <code>1 + 5 + 9 = 15</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[1,1,1],[1,1,1],[1,1,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>We can place the rooks in the cells <code>(0, 2)</code>, <code>(1, 1)</code>, and <code>(2, 0)</code> for a sum of <code>1 + 1 + 1 = 3</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= m == board.length &lt;= 100</code></li>
	<li><code>3 &lt;= n == board[i].length &lt;= 100</code></li>
	<li><code>-10<sup>9</sup> &lt;= board[i][j] &lt;= 10<sup>9</sup></code></li>
</ul>

from random import random
class Solution:
    def maximumValueSum(self, board: List[List[int]]) -> int:
        # Mine in a contest.
        largest_row = []
        guaranteed = Counter()
        largest_nns = [-inf, -inf, -inf]
        for r, row in enumerate(board):
            rxx = sorted([(val, c) for c, val in enumerate(row)])[-3:]
            for aaaaa, bbbbb in rxx:
                if aaaaa <= largest_nns[-1]:
                    continue
                elif aaaaa <= largest_nns[-2]:
                    largest_nns[-1] = aaaaa
                elif aaaaa <= largest_nns[-3]:
                    largest_nns[-2], largest_nns[-1] = aaaaa, largest_nns[-2]
                elif aaaaa > largest_nns[-3]:
                    largest_nns[-3], largest_nns[-2], largest_nns[-1] = aaaaa, largest_nns[-3], largest_nns[-2]
            if rxx[0][0] == rxx[1][0] == rxx[2][0]:
                if guaranteed[rxx[0][0]] == 3:
                    continue
                largest_row.append([(rxx[0][0], int(random() * 1_000_000))])
                guaranteed[rxx[0][0]] += 1
            else:
                largest_row.append(rxx)
        
        # print(largest_nns, guaranteed)
        self.max_possible = sum(largest_nns)
        temp_tot = 0
        while largest_nns:
            if guaranteed[largest_nns[-1]]:
                guaranteed[largest_nns[-1]] -= 1
                temp_tot += largest_nns.pop()
            else:
                break
        else:
            return temp_tot
        largest_row.sort(key=lambda x: min(-a for a, b in x))
        self.found = False
        @cache
        def _r(idx=0, curr=0, a=None, b=None):
            if idx == len(largest_row) or self.found:
                return -inf
            max_val = -inf
            for val, col in sorted(largest_row[idx]):
                if col is not a and col is not b:
                    if a is not None and b is not None:
                        max_val = max(max_val, curr + val)
                    elif a is not None:
                        max_val = max(max_val, _r(idx + 1, curr + val, a, col))
                    else:
                        max_val = max(max_val, _r(idx + 1, curr + val, col, b))
            if max_val == self.max_possible:
                self.found = True
                return max_val
            return max(max_val, _r(idx + 1, curr, a, b))
        return _r()
141285