<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, of equal length <code>n</code>.</p>

<p>In one operation, you can swap the values of any two indices of <code>nums1</code>. The <strong>cost</strong> of this operation is the <strong>sum</strong> of the indices.</p>

<p>Find the <strong>minimum</strong> total cost of performing the given operation <strong>any</strong> number of times such that <code>nums1[i] != nums2[i]</code> for all <code>0 &lt;= i &lt;= n - 1</code> after performing all the operations.</p>

<p>Return <em>the <strong>minimum total cost</strong> such that </em><code>nums1</code> and <code>nums2</code><em> satisfy the above condition</em>. In case it is not possible, return <code>-1</code>.</p>

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

<pre>
<strong>Input:</strong> nums1 = [1,2,3,4,5], nums2 = [1,2,3,4,5]
<strong>Output:</strong> 10
<strong>Explanation:</strong> 
One of the ways we can perform the operations is:
- Swap values at indices 0 and 3, incurring cost = 0 + 3 = 3. Now, nums1 = [4,2,3,1,5]
- Swap values at indices 1 and 2, incurring cost = 1 + 2 = 3. Now, nums1 = [4,3,2,1,5].
- Swap values at indices 0 and 4, incurring cost = 0 + 4 = 4. Now, nums1 =[5,3,2,1,4].
We can see that for each index i, nums1[i] != nums2[i]. The cost required here is 10.
Note that there are other ways to swap values, but it can be proven that it is not possible to obtain a cost less than 10.
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [2,2,2,1,3], nums2 = [1,2,2,3,3]
<strong>Output:</strong> 10
<strong>Explanation:</strong> 
One of the ways we can perform the operations is:
- Swap values at indices 2 and 3, incurring cost = 2 + 3 = 5. Now, nums1 = [2,2,1,2,3].
- Swap values at indices 1 and 4, incurring cost = 1 + 4 = 5. Now, nums1 = [2,3,1,2,2].
The total cost needed here is 10, which is the minimum possible.
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [1,2,2], nums2 = [1,2,2]
<strong>Output:</strong> -1
<strong>Explanation:</strong> 
It can be shown that it is not possible to satisfy the given conditions irrespective of the number of operations we perform.
Hence, we return -1.
</pre>

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

<ul>
	<li><code>n == nums1.length == nums2.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums1[i], nums2[i] &lt;= n</code></li>
</ul>

class Solution:
    def minimumTotalCost(self, nums1: List[int], nums2: List[int]) -> int:
        # set result to 0. Result tracks the cost of the operations of the movements. 
        result = 0 
        # track frequency of numeric matches of nums in nums1 that are in nums2 at same index 
        base_frequency = collections.defaultdict(int)
        # track total matches 
        total = 0 
        # track most frequent num 
        most_frequent = 0 
        # track max frequency of most frequent num 
        max_frequency = 0 
        # enumerate nums zipped 
        for index, (num1, num2) in enumerate(zip(nums1, nums2)) : 
            # on match 
            if num1 == num2 : 
                # increment result by index, total by 1 and base frequency by 1 at num1 
                result += index 
                total += 1 # total is the total number of matches 
                base_frequency[num1] += 1 
                # if the base frequency of this value is now greater than max frequency 
                if base_frequency[num1] > max_frequency : 
                    # set the max frequency and most frequent accordingly 
                    max_frequency = base_frequency[num1]
                    most_frequent = num1 
        # if twice the max frequency is less than the total matches, return result of index summation of matches 
        # this is the cost as we can always get away with cost by half via interesting swaps 
        if 2 * max_frequency <= total : 
            return result 
        # otherwise, while you have a remainder, loop over nums again 
        # remainder in this case is the twice of the max frequency less the number of total matches 
        remainder = 2 * max_frequency - total 
        # and at the points where these do not match and neither are the most frequent value, increment the result by indexing and reduce remainder
        for index, (num1, num2) in enumerate(zip(nums1, nums2)) : 
            if num1 != num2 and num1 != most_frequent and num2 != most_frequent : 
                result += index 
                remainder -= 1 
            if remainder == 0 : 
                # if you ever run out of remainder, you have your result, as this is now the cost through swapping 
                return result 
        # otherwise too much remaining values of the most frequent number existed and it is not possible 
        return -1
35100