<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:
        # need to do O(n)
        # just need their counters to be equal
        # go through each mismatched count, make m swaps to equalize
        # 5: 4 and 5: 2 -> needs 1 swap
            # 5: 4, 5:1 -> can't make equal
            # is there ever a case of "inefficient swapping"?
        c1 = Counter(basket1)
        c2 = Counter(basket2)
        swaps = 0
        bal = 0
        all_vals = set(c1.keys())
        all_vals.update(set(c2.keys()))
        swaps_a = []
        swaps_b = []
        for c in sorted(all_vals):
            v = c1[c]
            v2 = c2[c]
            if v == v2:
                continue
            diff = abs(v-v2)
            if (diff) % 2 != 0:
                return -1
            # determining cost:
                # a: 2 2's, 3 4's
                # b: 4 3's, 1 5
                    # swap the 5 with a 2, rest are 3's
                    # ie, swap max with mins, and so on?
            # print('swapping', c, v, v2, diff//2 * (-1 if v < v2 else 1))
            bal += diff//2 * (-1 if v < v2 else 1)
            if v > v2:
                swaps_a.extend([c]*(diff//2))
            else:
                swaps_b.extend([c]*(diff//2))
        # print(bal)
        if bal != 0:
            return -1
        print(swaps_a)
        print(swaps_b)
        swap_cost = 0
        n = len(swaps_a)
        min_val = min(min(basket1), min(basket2))
        for i in range(n):
            # can get better swap cost by swapping w a small item, then swapping it back later
                # [43, 80, 88]
                # [32, 42, 68]
                    # swap 88 with 8 -> 8
                    # swap 68 with 8 -> 8
                    # ... -> 8*6
            raw_cost = min(swaps_a[i], swaps_b[n-i-1])
            swap_cost += min(raw_cost, min_val*2)
        return swap_cost

46100