<p>There is a survey that consists of <code>n</code> questions where each question&#39;s answer is either <code>0</code> (no) or <code>1</code> (yes).</p>

<p>The survey was given to <code>m</code> students numbered from <code>0</code> to <code>m - 1</code> and <code>m</code> mentors numbered from <code>0</code> to <code>m - 1</code>. The answers of the students are represented by a 2D integer array <code>students</code> where <code>students[i]</code> is an integer array that contains the answers of the <code>i<sup>th</sup></code> student (<strong>0-indexed</strong>). The answers of the mentors are represented by a 2D integer array <code>mentors</code> where <code>mentors[j]</code> is an integer array that contains the answers of the <code>j<sup>th</sup></code> mentor (<strong>0-indexed</strong>).</p>

<p>Each student will be assigned to <strong>one</strong> mentor, and each mentor will have <strong>one</strong> student assigned to them. The <strong>compatibility score</strong> of a student-mentor pair is the number of answers that are the same for both the student and the mentor.</p>

<ul>
	<li>For example, if the student&#39;s answers were <code>[1, <u>0</u>, <u>1</u>]</code> and the mentor&#39;s answers were <code>[0, <u>0</u>, <u>1</u>]</code>, then their compatibility score is 2 because only the second and the third answers are the same.</li>
</ul>

<p>You are tasked with finding the optimal student-mentor pairings to <strong>maximize</strong> the<strong> sum of the compatibility scores</strong>.</p>

<p>Given <code>students</code> and <code>mentors</code>, return <em>the <strong>maximum compatibility score sum</strong> that can be achieved.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]]
<strong>Output:</strong> 8
<strong>Explanation:</strong>&nbsp;We assign students to mentors in the following way:
- student 0 to mentor 2 with a compatibility score of 3.
- student 1 to mentor 0 with a compatibility score of 2.
- student 2 to mentor 1 with a compatibility score of 3.
The compatibility score sum is 3 + 2 + 3 = 8.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[1,1],[1,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The compatibility score of any student-mentor pair is 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == students.length == mentors.length</code></li>
	<li><code>n == students[i].length == mentors[j].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 8</code></li>
	<li><code>students[i][k]</code> is either <code>0</code> or <code>1</code>.</li>
	<li><code>mentors[j][k]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

# Data Structure: 2D Arrays for students and mentors, Backtracking for generating pairings
# Algorithm: Backtracking to explore all possible pairings and calculate the sum of compatibility scores

# Short Intuition:
# We need to assign students to mentors in a way that maximizes the compatibility score.
# Backtracking is used to explore all possible student-mentor pairings and pick the one with the highest score.

# Detailed Intuition:
# - The problem can be viewed as finding the best way to pair students with mentors to maximize compatibility.
# - The compatibility score is calculated by comparing each student's answers with each mentor's answers.
# - We need to explore all possible pairings (permutations of mentor-student assignments).
# - For each pairing, calculate the total compatibility score.
# - Use backtracking to explore all pairings and track the one with the highest score.
# - This brute-force approach is feasible due to the limited size of m (up to 8).

# Pseudo Code:
# 1. Define a function to calculate compatibility between a student and a mentor by comparing their answers.
# 2. Use backtracking to generate all possible pairings.
# 3. For each pairing, calculate the total compatibility score by summing individual scores for each pair.
# 4. Track the maximum compatibility score found during the backtracking process.
# 5. Return the maximum score.

# Example for complication: For each student, pair them with every mentor and calculate the total score, keeping track of the maximum sum.

from itertools import permutations

class Solution:
    # Function to calculate the compatibility score between a student and a mentor
    def calculate_compatibility(self, student, mentor):
        # Compare the answers of the student and mentor
        score = 0
        for i in range(len(student)):  # Loop through each question
            if student[i] == mentor[i]:  # If answers match
                score += 1  # Increase compatibility score
        return score  # Return the compatibility score

    # Main function to find the maximum compatibility score sum
    def maxCompatibilitySum(self, students: List[List[int]], mentors: List[List[int]]) -> int:
        m = len(students)  # Number of students (and mentors)
        max_score = 0  # Initialize maximum score as 0

        # Step 1: Generate all possible pairings using permutations
        for perm in permutations(range(m)):  # All permutations of mentor indices
            total_score = 0  # Total score for the current permutation
            for student_idx, mentor_idx in enumerate(perm):  # Loop through each student-mentor pairing
                total_score += self.calculate_compatibility(students[student_idx], mentors[mentor_idx])
            max_score = max(max_score, total_score)  # Track the maximum score

        # Step 2: Return the maximum compatibility score found
        return max_score

# Time Complexity: O(m! * m * n), where m is the number of students/mentors, n is the number of questions.
# Explanation: There are m! permutations to check, and for each permutation, we compare m pairs of student-mentor answers over n questions.

# Space Complexity: O(m), due to the space needed to store permutations and the recursion stack used in backtracking.

1652