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

import numpy as np
from scipy.optimize import linear_sum_assignment
from typing import List
from heapq import nlargest

class Solution:
    def maximumValueSum(self, board: List[List[int]]) -> int:
        m, n = len(board), len(board[0])
        
        # Handle small matrices
        if m < 3 or n < 3:
            return 0
        
        # Convert board to a numpy array for efficient operations
        board_np = np.array(board)
        
        # Function to get the top k rows based on the sum of their maximum values
        def get_top_rows(board_np, k):
            row_sums = np.max(board_np, axis=1)
            top_rows_indices = np.argsort(row_sums)[-k:]
            return top_rows_indices

        # Get top 30 rows based on their maximum values
        top_row_indices = get_top_rows(board_np, 30)
        
        # Initialize max_sum
        max_sum = float('-inf')
        
        # Iterate over all combinations of 3 rows from the top row indices
        for row1, row2, row3 in combinations(top_row_indices, 3):
            # Create the cost matrix for the selected rows
            cost_matrix = np.array([
                board_np[row1],
                board_np[row2],
                board_np[row3]
            ], dtype=float)

            # Solve the assignment problem using the Hungarian algorithm
            row_ind, col_ind = linear_sum_assignment(-cost_matrix)
            
            # Calculate the total sum for this assignment
            current_sum = cost_matrix[row_ind, col_ind].sum()
            
            # Update max_sum if necessary
            max_sum = max(max_sum, current_sum)
        
        return int(max_sum)

70646