<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 maxScore(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])
        scores = sorted([(-grid[i][j], 1 << i ) for i in range(m) for j in range(n)])
        score_len = len(scores)
        dp = {}
        def explore(i, taken_row):
            key = (i, taken_row)
            if i >= score_len:
                return 0

            if key in dp:
                return dp[key]
            
            s, row = scores[i]

            a = 0
            if (taken_row & row == 0):
                nxt = i + 1
                while nxt < score_len and (scores[nxt][0] == s or scores[nxt][1] == row):
                    nxt += 1
                a = -s + explore(nxt, taken_row | row) 

            
            nxt = i + 1
            while nxt < score_len and scores[nxt][0] == s and scores[nxt][1] == row:
                nxt += 1
            b = explore(nxt, taken_row)
            
            dp[key] = max(a, b)
            return dp[key]

        return explore(0,0)



    def maxScore_bf(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])

        top_vals = []

        for i in range(m):
            vals = set()
            heap = []
            for j in range(n):
                if grid[i][j] not in vals:
                    vals.add(grid[i][j])
                    if len(heap) < m:
                        heappush(heap, grid[i][j])
                    else:
                        heappushpop(heap, grid[i][j])
            top_vals.append(set(heap))
        #print(top_vals)
        max_sum = 0
        cur_set = set()
        def explore(i, c_sum):
            #print(c_sum)
            nonlocal max_sum
            for j in top_vals[i]:
                if i < (m - 1):
                    if j not in cur_set:
                        cur_set.add(j)
                        explore(i + 1, c_sum + j)
                        cur_set.remove(j)
                else:
                    if j not in cur_set:
                        max_sum = max(max_sum, c_sum + j)
                    else:
                        max_sum = max(max_sum, c_sum)
            if i < (m - 1):
                explore(i + 1, c_sum)
            else:
                max_sum = max(max_sum, c_sum)

        explore(0, 0)

        return max_sum

        
108800