Backtracking with Dp and Bit Manipulation || explanation in comments || C++
\nclass Solution {\npublic:\n     int dp[10][18000];\n    int helper(int idx,int taken,vector<vector<int>>& student,vector<vector<int>>& mentor)\n    {\n       
```\nclass Solution {\npublic:\n     int dp[10][18000];\n    int helper(int idx,int taken,vector<vector<int>>& student,vector<vector<int>>& mentor)\n    {\n        if(idx>=student.size()) return 0;\n        if(dp[idx][taken]!=-1) return dp[idx][taken];\n        int ans=-1e8;\n        for(int i=0;i<student.size();i++)\n        {\n            //taken will tell us about the indexes that have been taken, e.g. taken=000101 in bit reprenstation tells index 0 and 2 have been taken\n            if(taken&(1<<i)) continue; //if this index is already taken then continue\n            taken^=(1<<i);             //take the current index ,xor will  set the bit  at index i\n            int curr=0;\n            for(int k=0;k<student[idx].size();k++){ //calculate current score\n                if(student[idx][k]==mentor[i][k]) curr++;\n            }\n            ans=max(ans,(curr)+helper(idx+1,taken,student,mentor));//take maximum of all possibilities\n            taken^=(1<<i); //backtrack the previous taken index, xor will reset the bit at index i\n        }\n        return dp[idx][taken]=ans;\n    }\n    int maxCompatibilitySum(vector<vector<int>>& student, vector<vector<int>>& mentor) {\n        int n=student.size();int q=student[0].size();\n        memset(dp,-1,sizeof(dp));\n        int ans=helper(0,0,student,mentor);\n        return ans;\n    }\n};\n```
[]