<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:
        nums = sorted(list(set(basket1 + basket2)))
        freq = {}
        for num in basket1:
            if num not in freq:
                freq[num] = 0
            freq[num] += 1
        
        for num in basket2:
            if num not in freq:
                freq[num] = 0
            freq[num] -= 1
        
        for num in freq:
            if abs(freq[num]) % 2 == 1:
                return -1
        
        n = len(nums)
        l = 0
        r1 = n - 1
        while r1 >= 0 and freq[nums[r1]] <= 0:
            r1 -= 1
        
        r2 = n - 1
        while r2 >= 0 and freq[nums[r2]] >= 0:
            r2 -= 1
        
        min_cost = 0
        while (l < r1 or l < r2) and (freq[nums[l]] == 0 or nums[l] < 2 * nums[0]):
            n_s = freq[nums[l]] # num of swap with nums[l], actually nums[l] // 2
            if n_s == 0:
                l += 1
                continue
            if n_s > 0:
                r_s = freq[nums[r2]]
                if abs(r_s) <= n_s:
                    freq[nums[r2]] = 0
                    freq[nums[l]] += r_s
                    min_cost += nums[l] * abs(r_s) // 2
                    r2 -= 1
                    while r2 >= 0 and freq[nums[r2]] >= 0:
                        r2 -= 1

                else:
                    min_cost += nums[l] * n_s // 2
                    freq[nums[l]] = 0
                    freq[nums[r2]] += n_s
                    l += 1
            else:
                r_s = freq[nums[r1]]
                if r_s <= abs(n_s):
                    freq[nums[r1]] = 0
                    freq[nums[l]] += r_s
                    min_cost += nums[l] * r_s // 2
                    r1 -= 1
                    while r1 >= 0 and freq[nums[r1]] <= 0:
                        r1 -= 1
                else:
                    min_cost += nums[l] * abs(n_s) // 2
                    freq[nums[l]] = 0
                    freq[nums[r1]] += n_s
                    l += 1
            
        r = max(r1,r2)

        while l <= r:
            min_cost += nums[0] * abs(freq[nums[r]]) // 2
            r -= 1
        
        return min_cost



            




            
        


                    

        
594