Bitmask DP | JAVA
\n\nclass Solution {\n    Integer[][]dp;\n    int m, n;\n    int[][] S;\n    int[][] M;\n    public int maxCompatibilitySum(int[][] students, int[][] mentors) {
```\n\nclass Solution {\n    Integer[][]dp;\n    int m, n;\n    int[][] S;\n    int[][] M;\n    public int maxCompatibilitySum(int[][] students, int[][] mentors) {\n        m = students.length;\n        n = students[0].length;\n        S = students;\n        M = mentors;\n        dp = new Integer[m][1 << 8];\n        return dfs(0, 0);\n    }\n\n    // m : no of students\n    // n : no of Questions\n    public int dfs(int i, int mask){\n        if(i == m)\n            return 0;\n        \n        if(dp[i][mask] != null) return dp[i][mask];\n        \n        int maxScore = 0;\n        for (int j = 0; j < m; j++){\n            if((mask & (1 << j)) == 0){\n                int score = 0;\n                for (int k = 0; k < n; k++){\n                    score += S[i][k] == M[j][k] ? 1 : 0;\n                }\n                score += dfs(i + 1, mask | (1 << j));\n                maxScore = Math.max(maxScore, score);\n            }\n        }\n        dp[i][mask] = maxScore;\n        return maxScore;\n    }\n}\n```
[]