<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>

class Solution:
    def maximumValueSum(self, board: List[List[int]]) -> int:

        # helper(rook, r, colset) should return the max value when placing ith rook on rth row and in a column not added to colset already
        # that way, max([helper(0, r, set()) for r in range(rows - 2)]) should give the ans
        @cache
        def helper(rook_index, first_possible_row, c1, c2):
            if rook_index == 3:
                # all three rooks 0, 1, 2 are placed
                return 0
            
            # if dp[rook_index][first_possible_row][c1][c2] != -1:
            #     return dp[rook_index][first_possible_row][c1][c2]

            curr_max = float('-inf')
            for r in range(first_possible_row, rows + rook_index - 2):
                for c in range(3):
                    curr_col = new_board[r][c][1]
                    if curr_col not in (c1, c2):
                        curr_val = 0
                        if rook_index == 0:
                            curr_val = helper(rook_index + 1, r + 1, curr_col, c2)
                        elif rook_index == 1:
                            curr_val = helper(rook_index + 1, r + 1, c1, curr_col)
                        curr_max = max(curr_max, new_board[r][c][0] + curr_val)

            # dp[rook_index][first_possible_row][c1][c2] = curr_max
            
            return curr_max



        rows, cols = len(board), len(board[0])
        # dp = [[[[-1] * (cols+1) for _ in range(cols+1)] for i in range(rows)] for j in range(3)]
        # print(rows, cols)
        new_board = []

        for row in board:
            curr_row = sorted([(x, index) for index, x in enumerate(row)], reverse=True)[:3]
            new_board.append(curr_row)
        
        return helper(0, 0, cols, cols)










        # need to cache:
            # prev_row
            # all cols
            # rook_index

        
        # def helper(prev_row, rook_index):
        #     if rook_index == 3:
        #         return 0

        #     first_row = 0 if not rowList else rowList[-1] + 1
        #     curr_max = float("-inf")
        #     for r in range(first_row, rows + rook_index - 2):
        #         for c in range(cols):
        #             if c not in colSet:
        #                 rowList.append(r)
        #                 colSet.add(c)
        #                 curr_max = max(curr_max, board[r][c] + helper(rook_index + 1))
        #                 colSet.remove(c)
        #                 rowList.pop()
        #     return curr_max

        # rows, cols = len(board), len(board[0])
        # rowList, colSet = [], set()
        # return helper(0)


        # def helper(first_possible_row, rook_index):
        #     if rook_index == 3:
        #         return 0

        #     curr_max = float("-inf")

        #     for r in range(first_possible_row, rows + rook_index - 2):
        #         for c in range(cols):
        #             if c not in colSet:
        #                 colSet.add(c)
        #                 curr_max = max(curr_max, board[r][c] + helper(r + 1, rook_index + 1))
        #                 colSet.remove(c)

        #     return curr_max

        # rows, cols = len(board), len(board[0])
        # colSet = set()
        # return helper(0,0)
39934