Python DP, easy to understand with comments
\n\n# Code\npython3 []\nimport collections\nclass Solution:\n  def maxScore(self, grid: List[List[int]]) -> int:\n    n = len(grid)\n    # DP goal: choose the r
\n\n# Code\n```python3 []\nimport collections\nclass Solution:\n  def maxScore(self, grid: List[List[int]]) -> int:\n    n = len(grid)\n    # DP goal: choose the row that equals to given number, so it makes score max\n    memo = [[-1] * (1<<n) for _ in range(101)]\n    # number to row (to easily find all rows with this number, can be found by sorting and iterating)\n    num_to_row = collections.defaultdict(list) \n    mx_num = 0\n    for i in range(len(grid)):\n      for j in range(len(grid[i])):\n        mx_num = max(mx_num, grid[i][j])\n        num_to_row[grid[i][j]].append(i)\n    def dp(num, mask):\n      if num == 0:\n        return 0\n      if memo[num][mask] != -1:\n        return memo[num][mask]\n      # skip\n      mx = dp(num - 1, mask)\n      # take\n      for row in num_to_row[num]:\n        if mask & (1 << row) != 0:\n          continue\n        mx = max(mx, num + dp(num - 1, mask | (1 << row)))\n\n      memo[num][mask] = mx\n      return memo[num][mask]\n    return dp(mx_num, 0)\n      \n```
['Python3']