<p>You have two fruit baskets containing <code>n</code> fruits each. You are given two <strong>0-indexed</strong> integer arrays <code>basket1</code> and <code>basket2</code> representing the cost of fruit in each basket. You want to make both baskets <strong>equal</strong>. To do so, you can use the following operation as many times as you want:</p>

<ul>
	<li>Chose two indices <code>i</code> and <code>j</code>, and swap the <code>i<font size="1">th</font>&nbsp;</code>fruit of <code>basket1</code> with the <code>j<font size="1">th</font></code>&nbsp;fruit of <code>basket2</code>.</li>
	<li>The cost of the swap is <code>min(basket1[i],basket2[j])</code>.</li>
</ul>

<p>Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.</p>

<p>Return <em>the minimum cost to make both the baskets equal or </em><code>-1</code><em> if impossible.</em></p>

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

<pre>
<strong>Input:</strong> basket1 = [4,2,2,2], basket2 = [1,4,1,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
</pre>

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

<pre>
<strong>Input:</strong> basket1 = [2,3,4,1], basket2 = [3,2,5,1]
<strong>Output:</strong> -1
<strong>Explanation:</strong> It can be shown that it is impossible to make both the baskets equal.
</pre>

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

<ul>
	<li><code>basket1.length == basket2.length</code></li>
	<li><code>1 &lt;= basket1.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= basket1[i],basket2[i]&nbsp;&lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minCost(self, basket1: List[int], basket2: List[int]) -> int:
        # It's not enough that the two baskets have the same sum at the end.
        # They are equal, if and only if, they are exactly the same after sorting.
        # For example, a=[1,1,1,7] and b=[2,2,2,4] each add up to 10, but they do not have the same
        # values in each index after ordering. Hence, these baskets are not considered to be equal in this case
        # and hence we would return -1 in this case

        # Hence, since we must have the exact same characters in each index in each array, we must have 
        # the exact same characters in each array with the exact same frequency.
        # For example, if we have 3 5's, then we can't distribute them evenly (3/2 == 1.5)

        # So, if one basket has 3 1's and the other has a single 1, then we need to move a single one to the
        # second array

        freq1 = Counter(basket1)

        # Subtract the frequency for each number. Tells us the difference in frequencies between the two baskets, for each number
        for fruit in basket2:
            freq1[fruit] -= 1

        to_move = []

        for fruit, count in freq1.items():
            # If the difference is odd, then we can't split this number evenly between the 2 baskets
            if count % 2 != 0:
                return -1

            # For the difference in each fruit, we will need to move half of them to the other basket
            to_move += [fruit] * abs(count // 2)

        # Get the cost of doing indirect transfer, by using the cheapest fruit among the two baskets
        indirect = 2 * min(basket1 + basket2)

        # We want to use the cheapest fruits to move
        to_move.sort()

        min_cost = 0
        for index in range(len(to_move) // 2):
            min_cost += min(indirect, to_move[index])

        return min_cost
605