<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p>

<p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p>

<ul>
	<li>No two selected cells are in the <strong>same</strong> row of the matrix.</li>
	<li>The values in the set of selected cells are <strong>unique</strong>.</li>
</ul>

<p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p>

<p>Return the <strong>maximum</strong> score you can achieve.</p>

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

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

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

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

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p>

<p>We can select the cells with values 1, 3, and 4 that are colored above.</p>
</div>

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

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

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

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

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p>

<p>We can select the cells with values 7 and 8 that are colored above.</p>
</div>

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

<ul>
	<li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li>
	<li><code>1 &lt;= grid[i][j] &lt;= 100</code></li>
</ul>


class Solution:

    def __init__(self):
        self.max_sum = 0
        self.rows = 0
        self.number_index_dict = {}
        self.dp = {}
    
    def bruteforce(self, rows_visited, current_pos):
        if (rows_visited, current_pos) in self.dp:
            return self.dp[(rows_visited, current_pos)]

        if rows_visited == (1 << self.rows) - 1 or current_pos == len(self.all_numbers):
            # print(row_visited, current_sum, current_pos)
            return 0

        current_number = self.all_numbers[current_pos]
        mask = self.number_index_dict[current_number]

        # considering the current number

        index = 0
        current_max_sum = 0
        while mask:
            if (mask & 1) and rows_visited & (1 << index) == 0:
                new_rows_visited = rows_visited | (1 << index)
                current_max_sum = max(current_max_sum, current_number + self.bruteforce(new_rows_visited, current_pos + 1))
            mask >>= 1
            index += 1

        # not considering the current number
        self.dp[(rows_visited, current_pos)] = max(self.bruteforce(rows_visited, current_pos + 1), current_max_sum)

        return self.dp[(rows_visited, current_pos)]

    def maxScore(self, grid: List[List[int]]) -> int:
        # row_visited = [0 for i in range(len(grid))]
        # visited_rows = set()
        self.rows = len(grid)
        for row, row_items in enumerate(grid):
            for number in row_items:
                mask = self.number_index_dict.get(number, 0)
                self.number_index_dict[number] = mask | (1 << row)

        self.all_numbers = list(self.number_index_dict.keys())
        self.all_numbers.sort(reverse=True)
        # print(f"all numbers {self.all_numbers} numbers dict {self.number_index_dict}")
        return self.bruteforce(0, 0)


25920