Optimized Solution with Detailed Explanation: Beats 100% in Runtime and 64.71% in Memory
Intuition\n Describe your first thoughts on how to solve this problem. \nFor each unique value, store the rows in which it appears. We now have a maximum of \uD
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor each unique value, store the rows in which it appears. We now have a maximum of $$\uD835\uDC41\xD7\uD835\uDC40$$ unique values, where for each value, we can have at most $$N$$ rows. Here, $$N$$ represents the number of rows, and $$M$$ represents the number of columns.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe use Dynamic Programming (DP) with bitmasks, where the bitmask represents the rows. For each value, we attempt to select it from a row that hasn\'t been used yet.\n# Complexity\n- Time complexity: $$O(N\xD7M\xD72^N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(N\xD7M\xD72^N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    map<int,vector<int>> idx ;\n    vector<vector<int>>dp ;\n    int solve(int i,auto it1 , int taken_rows){\n        if(i==idx.size())\n            return 0 ;\n        int &res = dp[i][taken_rows] ;\n        if(~res)\n            return res ;\n        auto it2 = next(it1);\n        res = solve(i+1,it2,taken_rows);\n        auto &[num,rows] = *it1 ;\n        for(int r : rows){\n            if((1<<r)&taken_rows)\n                continue;\n            res = max(res,solve(i+1,it2,(1<<r)|taken_rows)+num);\n        }\n        return res ;\n    }\n    int maxScore(vector<vector<int>>& grid) {\n        int n= grid.size() , m = grid[0].size();\n        for(int i=0 ;i<n;i++)\n        {\n            for(int j =0 ;j<m;j++)\n                idx[grid[i][j]].emplace_back(i);\n        }\n        dp.resize(idx.size(),vector<int>((1<<n),-1));\n        return solve(0,idx.begin(),0);\n    }\n};\n```
['Dynamic Programming', 'Bitmask', 'C++']