No Bitmask, No DP, No Recursion, Just Pure Permutation (as given in hints)
This is a tricky problem since one doesn\'t (at least I) think about permutations when dealing with variations. However, proceedings on the lines of "Minimum Fa
This is a tricky problem since one doesn\'t (at least I) think about permutations when dealing with variations. However, proceedings on the lines of "[Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/)" encounters failure, as additional data structures make the intermittently-working program more complex, thereby depicting the complexity of the problem. It is not surprising, that the worst case for this problem corresponds to finding maximum path in a `8x8` matrix with each selected column not to be chosen again.\n\n```\nclass Solution {\npublic:\n    template<typename incoming, typename outgoing>\n    void load(incoming &in, outgoing &out) {\n        for(typename incoming::value_type &s: in) {\n            int res = 0, e = 0;\n            while(not s.empty()) {\n                res += s.back() * (std::pow(2, e)); s.pop_back();\n                ++e;\n            }\n            out.push_front(res);\n        }\n    }\n    \n    int maxCompatibilitySum(vector<vector<int>>& students, vector<vector<int>>& mentors) {\n        int const n = students.size(), m = students[0].size();\n        std::deque<int> g, h;\n        load(students, g); load(mentors, h);\n        std::sort(g.begin(), g.end());\n        int ans = 0;\n        std::function<int(int, int)> get_value = [&](int a, int b) {\n                                                        int res = a ^ b, diff = 0;\n                                                        while(res) {\n                                                            res = res & (res - 1);\n                                                            diff += 1;\n                                                        }\n                                                        return m - diff;\n                                                    };\n        \n        do {\n            int res = 0;\n            for(int i = 0; i < n; ++i) {\n                res += get_value(g[i], h[i]);\n            }\n            ans = std::max(res, ans);\n        } while(std::next_permutation(g.begin(), g.end()));\n        \n        return ans;\n    }\n};\n```\n![image](https://assets.leetcode.com/users/images/c5c4e254-2040-4d24-86bb-730484716a0c_1714660618.2001138.png)\n
['C', 'Probability and Statistics']