Mask DP
With m and n constrained to 8, it\'s feasible to just check all permutations.\n \nWe just check all possible assignments for each student i, using mask to track
With `m` and `n` constrained to `8`, it\'s feasible to just check all permutations.\n \nWe just check all possible assignments for each student `i`, using `mask` to track mentors that have been assigned. We track and return the maximum score.\n \nTo speed things up, we can use memoisation, where we store interim results for each `i` and `mask`.\n\n**C++**\n```cpp\nint dp[256] = {[0 ... 255] = -1};\nint maxCompatibilitySum(vector<vector<int>>& s, vector<vector<int>>& m, int i = 0, int mask = 0) {\n    if (i == s.size())\n        return 0;\n    if (dp[mask] < 0)\n        for (int j = 0; j < m.size(); ++j) {\n            if ((mask & (1 << j)) == 0)\n                dp[mask] = max(dp[mask], \n                    inner_product(begin(s[i]), end(s[i]), begin(m[j]), 0, plus<int>(), equal_to<int>())\n                    + maxCompatibilitySum(s, m, i + 1, mask + (1 << j)));\n        }\n    return dp[mask];\n}\n```
[]