Java take not take dp using bitmask , fastest beats 100%
\nclass Solution {\n    public int maxScore(List<List<Integer>> grid) {\n        List<List<Integer>> list = new ArrayList<>();\n        for (int row = 0; row < 
```\nclass Solution {\n    public int maxScore(List<List<Integer>> grid) {\n        List<List<Integer>> list = new ArrayList<>();\n        for (int row = 0; row < grid.size(); row++) {\n            for (int col = 0; col < grid.getFirst().size(); col++) {\n                list.add(List.of(row, col));\n            }\n        }\n        list.sort((l1, l2) -> {\n            int r1 = l1.getFirst(), c1 = l1.getLast();\n            int r2 = l2.getFirst(), c2 = l2.getLast();\n            return Integer.compare(grid.get(r1).get(c1),\n                    grid.get(r2).get(c2));\n        });\n        int n = list.size();\n        int[][] dp = new int[n + 1][1 << grid.size()];\n        for (int i = n - 1; i >= 0; i--) {\n            for (int mask = (1 << grid.size()) - 1; mask >= 0; mask--) {\n                int row = list.get(i).getFirst();\n                int col = list.get(i).getLast();\n                int val = grid.get(row).get(col);\n                dp[i][mask] = dp[i + 1][mask];\n                if (((mask >> row) & 1) == 0) {\n                    int l = i, r = n - 1, pos = r + 1;\n                    while (l <= r) {\n                        int m = (l + r) >> 1;\n                        int row_mid = list.get(m).getFirst();\n                        int col_mid = list.get(m).getLast();\n                        int val_mid = grid.get(row_mid).get(col_mid);\n                        if (val_mid > val) {\n                            pos = m;\n                            r = m - 1;\n                        } else l = m + 1;\n                    }\n                    dp[i][mask] = Math.max(dp[pos][mask ^ (1 << row)] + val, dp[i][mask]);\n                }\n            }\n        }\n        return dp[0][0];\n    }\n}\n```
['Sorting', 'Binary Tree', 'Bitmask', 'Java']