<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:
        # generate a sorted list of (value, row, col) in the grid.
        # this sorting will happen in lexographical order, just like
        # ordering something alphabetically. So, `value` will be the
        # most significant element, but if `value[i] == value[j], i != j`,
        # then the next most significant element is what row they
        # are in. 
        values = sorted([(grid[i][j], i, j) for i in range(len(grid)) for j in range(len(grid[0]))])

        dp = {}
        def solve(i, rows):
            # If we've tried all of the values, just exit.
            # no more points to be gained.
            if i >= len(values):
                return 0

            # if we've already solved the path at this point,
            # from this index with this combination of rows already
            # selected, then simply return that cached result
            if (i, rows) in dp:
                return dp[(i, rows)]

            # will store the result
            res = 0
            # Each value in values is a tuple of (value, row, col). So,
            # here we get the row from the value at the current index
            # for this recusion step.
            cur_row = values[i][1]

            # here, we check if we have already selected a value
            # from the current row. If so, we'll just move on to the
            # next value in the list, skipping over this one, since
            # this one can't be selected.
            if (rows & (1 << cur_row)):
                res = solve(i+1, rows)
            # If we haven't already selected a value from this row,
            # then we have the option to either select one or not select one.
            # so there are two potential paths in the else block.
            else:
                # get the index of the next unique value.
                j = i
                while (j < len(values)) and (values[i][0] == values[j][0]):
                    j += 1
                
                # here, we select this value and continue at the index
                # of the next unique value (since values is sorted, and
                # we can't select two of the same value, this will be
                # the next smallest value). We use the OR operator to
                # set the bit corresponding with the row of this element
                # we're selecting in the `rows` bitmask.
                path1 = values[i][0] + solve(j, rows | (1 << cur_row))

                # Otherwise, we can choose not to select this value and
                # just move on to the next value in the values list.
                path2 = solve(i+1, rows)

                # the result will be the maximum of the two paths,
                # since we want to maximize our score:
                res = max(path1, path2)
            
            # cache and return the result
            dp[(i, rows)] = res
            return res

        # thanks to https://leetcode.com/u/virujthakur01/ for inspiring
        # this solution. I adapted the python code here from his C++ solution,
        # and used his solution to understand the problem and gain
        # the necessary insight and intuition to solve it.
        return solve(0,0)
114720