bitmask dp O(2**(m)*(numofvals))
We have at most 100 unique values in the grid, so it is a good dp variable. Store the rows in which each value appears in a dictionary, and we loop over \'the s
We have at most 100 unique values in the grid, so it is a good dp variable. Store the rows in which each value appears in a dictionary, and we loop over \'the smallest value below which all numbers we have taken\' in the dp.\n\nThe other variable in the dp is a bit mask for the rows already used, which is at most 2^10 = 1024. For each value we are considering, first simply skip it, then if it is in the grid, consider every row where it appears, if not already covered in the mask, we may go with this branch, until we reached the upper limit of val -- 101, where we return 0.\n\n\n# Code\n```python3 []\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:\n        cells = {i:set([]) for i in range(101)}\n        m = len(grid)\n        n = len(grid[0])\n        for i in range(m):\n            for j in range(n):\n                cells[grid[i][j]].add((i))\n        @cache\n        def dp(val,mask):\n            if val == 101:\n                return 0\n            res = dp(val+1,mask)\n            if val not in cells:return res\n            for row in cells[val]:\n                if (1 << row) & mask == 0:\n                    res = max(res,val+dp(val+1,mask | (1<<row)))\n            return res\n        \n        return dp(0,0)\n\n\n\n        \n```
['Dynamic Programming', 'Bitmask', 'Python3']