<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:
        '''
        * make basket1 every index cost equal to basket 2 
        * can swap any index in basket 1 with basket 2 at the cost of min(basket[i], basket2[j])

        * when sorting both baskets.. every index should be the same cost.. 
        * return min swap cost to acheive this (if possible) else -1
        ======
        * think of each uniq cost as a fruit type.. 
        * if both basket contains same number of all types of fruits.. then I made it equal.. 

        * this means that, every fruit type should be in even counts.. because an odd count fruit type will always lead to a case where I will have it one basket but in the other and I will never be able to make it equal ( I must assign every fruit type to a basket)
        ======
        '''

        basket1_cnt = collections.Counter(basket1)
        basket2_cnt = collections.Counter(basket2)

        total_cnt = basket1_cnt + basket2_cnt 

        for k, v in total_cnt.items(): 
            if v%2 != 0: 
                return -1 # at least one of the fruit type is odd.. can never make both baskets equal.. 
            total_cnt[k] = total_cnt[k] // 2  # every item cnt // 2 should give me the pair count.. 

        print(total_cnt)

        ### which will be the lowest costing swap to keep the overall costs lower? anything that's swapped with lowest costing fruit because my cost is min(basket[i], basket2[j])
        lowest_costing_fruit = min(list(total_cnt.keys()))
        # ### try swapping this lowest cost fruit with every other fruit that's currently not in the basket.. or fruit with itself..
        not_in_c1 = basket1_cnt - total_cnt
        not_in_c2 = basket2_cnt - total_cnt

        spread_pairs = []
        for k, v in (not_in_c1 + not_in_c2).items(): 
            spread_pairs += [k] * v

        spread_pairs.sort() # sort from smallest costing to largest costing other fruits.. 

        ans = 0 
        for f in spread_pairs[:len(spread_pairs)//2]: # (f, f) pair in the first half of other pairs
            ans += min(f, 2 * lowest_costing_fruit) # swap f with f OR f with min_costing_fruit? 
        return ans
        
655