DP || bitmasking || C++.
IntuitionWe need to select numbers from a 2D grid such that no two numbers come from the same row, and each number in the selected set is unique. The goal is to
# Intuition
We need to select numbers from a 2D grid such that no two numbers come from the same row, and each number in the selected set is unique. The goal is to maximize the sum of selected numbers. Using recursive strategy with bitmasking, where the state of each row (whether a number from that row has been used or not) is tracked by a bitmask. Starting from the maximum possible value (100), we attempt to select a number from each row and recursively maximize the score, ensuring that numbers are not repeated across rows using the bitmask.

# Approach
We use dynamic programming (DP) with memoization. The dp[num][bitmask] table stores the maximum score achievable for the current state, where num is the current number being considered (from 100 down to 1), and bitmask represents which rows have already been used. The `helper` function recursively computes the maximum score by either skipping the current number or selecting it from available rows. The grid is processed by creating a mapping mp from each number to the rows in which it appears. Starting from the largest number, we try to maximize the score while ensuring that the same row isn't used multiple times using the bitmask.

# Complexity
- Time complexity:
O(100 * 2^m * m)

- Space complexity:
O(101 * 2^m + m * n)

# Code
```cpp []
class Solution {
public:
    int dp[101][1025];
    int maxScore(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        unordered_map<int, vector<int>> mp; // element present in rows
        memset(dp, -1, sizeof(dp));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                mp[grid[i][j]].push_back(i);
            }
        }
        return helper(grid, 0, 100, mp);//start from max value possible to accumulate max score
    }
    int helper(vector<vector<int>>& grid, int bitmask, int num,
             unordered_map<int, vector<int>>& mp) {
        if (num == 0)
            return 0;
        if (dp[num][bitmask] != -1)
            return dp[num][bitmask];
        int res = helper(grid, bitmask, num - 1, mp);
        for (auto row : mp[num]) {
            if (bitmask >> row & 1)
                continue;
            res =
                max(res, num + helper(grid, (bitmask | (1 << row)), num - 1, mp));
        }
        return dp[num][bitmask] = res;
    }
};

```
['C++']