<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:
        b1 = dict(Counter(basket1))
        b2 = dict(Counter(basket2))
        
        tot = Counter(basket1 + basket2)

        c1 = [] # accumulate the differences in the counter dict items in b1
        c2 = [] # accumulate the differences in the counter dict items in b2
        for k, v in tot.items(): # n complexity loop every op is O(1)
            if v % 2 != 0:
                return -1
            b1c = b1.get(k, 0) # safe get key value
            b2c = b2.get(k, 0)

            #b1c and b2c are the value of the basket counter 
            if b1c == b2c:
                continue # optimization
            if b1c > b2c: # if counter at b1 is more than b2
                for i in range((b1c - b2c) // 2): # c1 gets the diff in number of 
                    c1.append(k)
            else:
                for i in range((b2c - b1c) // 2): # c2 gets the diff
                    c2.append(k)

    
        c1 = sorted(c1) # n log n worst case
        c2 = sorted(c2, reverse=True) # n log n worst case

        # were swapping min vals for max vals in every case


        ans = 0
        min_v = min(basket1 + basket2) # min total value for double swapping 
        for v1,v2 in zip(c1,c2):
            ans += min(v1,v2, min_v*2)
        return ans
    
    
    
    
    
    
    
    
    
    
    # def minCost(self, basket1: List[int], basket2: List[int]) -> int:
    #     b1 = dict(Counter(basket1))
    #     b2 = dict(Counter(basket2))
        
    #     d = b1 + b2
        
            

    #     # possible from this point forward.
    #     # print(d)
    #     optimal = []
    #     for key in d:
    #         if d[key] % 2 != 0:
    #             return -1
    #         for i in range(int(d[key] / 2)):
    #             optimal.append(key)
        

    #     # print(optimal)

    #     remainder = []
    #     remainder2 = []

    #     for obj in optimal:

    #         if obj in basket1:
    #             basket1.remove(obj)
    #         else:
    #             remainder.append(obj)

    #         if obj in basket2:
    #             basket2.remove(obj)
    #         else:
    #             remainder2.append(obj)
        
    #     # print(remainder, basket1, remainder2, basket2)

    #     weirdtotal = 0
    #     weirdtotal2 = 0

    #     for i in remainder:
    #         weirdtotal += min(i, tiny * 2)

            
    #     for i in basket1:
    #         weirdtotal2 += min(i, tiny * 2)
    #     # print(weirdtotal2)

    #     return min(weirdtotal, weirdtotal2)
        
48100