Easy bitmask and backtracking
Why backtracking\nThe contraints are only till 8, so we can explore all the paths.\nWhy bitmask\nRecursive call consumes less space, so, it is easy to keep trac
**Why backtracking**\nThe contraints are only till 8, so we can explore all the paths.\n**Why bitmask**\nRecursive call consumes less space, so, it is easy to keep track of which mentor is assigned and which isn\'t.\n\n```\n    /**\n        Find compatablilty scores for each student to each mentor.\n        \n        Try to explore all the possibilites of assigning a student to a mentor.\n        Start with "mask = 0", means, all bits are 0 in 0, means all mentors are free.\n        "mask and (1 shl j) == 0", means, is \'jth\' bit in the mask is \'0\'?, means, \'jth\' mentor is free.\n            - assign the mentor and explore other paths\n            - new mask will be updated as "mask or (1 shl j)", means, we have to set the \'jth\' bit in mask\n    **/\n    \n    fun maxCompatibilitySum(A: Array<IntArray>, B: Array<IntArray>): Int {\n        val n = A.size\n        val compat = Array(n) { IntArray(n) }\n        var ans = 0\n        for ((i, row1) in A.withIndex()) {\n            for ((j, row2) in B.withIndex()) {\n                var score = 0\n                for (k in 0 until row1.size)\n                    score += if (row1[k] == row2[k]) 1 else 0 // if both are same, score increases\n                compat[i][j] = score // ith student with jth mentor\n            }\n        }\n        fun dfs(i: Int, mask: Int, total: Int) {\n            if (i == n) { // all students are explored\n                ans = maxOf(ans, total)\n                return\n            }\n            for (j in 0 until n) { // explore all mentors for \'ith\' student\n                if (mask and (1 shl j) == 0) // is \'jth\' mentor free?\n                    dfs(i + 1, mask or (1 shl j), total + compat[i][j])\n            }\n        }\n        \n        dfs(0, 0, 0)\n        return ans\n    }\n```
['Bitmask', 'Kotlin']