IMAGINE IN SORTED 1D ARRAY | EASY BIT-MASKING | C++
Approach\nSo this one is a tricky one. Brute force suggests to try all possibilities on rows and maintain uniqueness through sets, which results in tle.\n\nTry 
# Approach\nSo this one is a tricky one. Brute force suggests to try all possibilities on rows and maintain uniqueness through sets, which results in tle.\n\nTry Imagining the problem for 1D Array, where you have to select some elements which are unique.\n\nFor that you can sort the array & use pick/not-pick.\n\nTo keep track of rows so that at-most 1 element from a row is selected. You can use bit-mask bcz max no. of rows is 10 (2^10 = 1024).\n\nYou should attach its row-no. to each element while converting in into 1D(use vec-pair or 1x2 dimension vec).\n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(2^n * n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(2^n * n^2)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n    int visAll;\n    vector<vector<int>> dp;\n\n    int solve(int idx, int mask, vector<pair<int,int>>&val){\n        if(idx==val.size()){return 0;}\n        if(mask==visAll){return 0;}\n        if(dp[idx][mask]!=-1){return dp[idx][mask];}\n\n        int currVal = val[idx].first, nxtUniqueIdx = idx;\n        while(val[nxtUniqueIdx].first==currVal){nxtUniqueIdx++; if(nxtUniqueIdx==val.size()){break;}}\n\n        int ans = solve(nxtUniqueIdx, mask, val);\n\n        for(int i=idx; i<nxtUniqueIdx; i++){\n            int curRow = val[i].second;\n            if((mask&(1<<curRow))==0){  // if row isn\'t vis\n                int tmp = solve(nxtUniqueIdx, mask|(1<<curRow), val) + val[i].first;\n                ans = max(ans, tmp);\n            }\n\n        }\n        \n        return dp[idx][mask] = ans;\n    }\n\n    int maxScore(vector<vector<int>>& grid){\n        int n = grid.size(), m = grid[0].size();\n\n        vector<pair<int,int>> val;\n        for(int i=0; i<n; i++){\n            for(int j=0; j<m; j++){\n                val.push_back({grid[i][j], i});\n            }\n        }\n\n        sort(val.begin(), val.end());\n        visAll = (1<<n) - 1;\n        dp.resize(val.size(), vector<int>((1<<n), -1));\n        return solve(0, 0, val);\n    }\n};\n```
['Array', 'Divide and Conquer', 'Dynamic Programming', 'Backtracking', 'Bit Manipulation', 'C', 'Matrix', 'Bitmask', 'C++']