<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> consisting of <code>3 * n</code> elements.</p>

<p>You are allowed to remove any <strong>subsequence</strong> of elements of size <strong>exactly</strong> <code>n</code> from <code>nums</code>. The remaining <code>2 * n</code> elements will be divided into two <strong>equal</strong> parts:</p>

<ul>
	<li>The first <code>n</code> elements belonging to the first part and their sum is <code>sum<sub>first</sub></code>.</li>
	<li>The next <code>n</code> elements belonging to the second part and their sum is <code>sum<sub>second</sub></code>.</li>
</ul>

<p>The <strong>difference in sums</strong> of the two parts is denoted as <code>sum<sub>first</sub> - sum<sub>second</sub></code>.</p>

<ul>
	<li>For example, if <code>sum<sub>first</sub> = 3</code> and <code>sum<sub>second</sub> = 2</code>, their difference is <code>1</code>.</li>
	<li>Similarly, if <code>sum<sub>first</sub> = 2</code> and <code>sum<sub>second</sub> = 3</code>, their difference is <code>-1</code>.</li>
</ul>

<p>Return <em>the <strong>minimum difference</strong> possible between the sums of the two parts after the removal of </em><code>n</code><em> elements</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [3,1,2]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Here, nums has 3 elements, so n = 1. 
Thus we have to remove 1 element from nums and divide the array into two equal parts.
- If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two parts will be 1 - 2 = -1.
- If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two parts will be 3 - 2 = 1.
- If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two parts will be 3 - 1 = 2.
The minimum difference between sums of the two parts is min(-1,1,2) = -1. 
</pre>

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

<pre>
<strong>Input:</strong> nums = [7,9,5,8,1,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The difference in sums will be (7+9) - (1+3) = 12.
To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) - (8+3) = 1.
It can be shown that it is not possible to obtain a difference smaller than 1.
</pre>

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

<ul>
	<li><code>nums.length == 3 * n</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def minimumDifference(self, A: List[int]) -> int:
        n = len(A) // 3
        
        # Build pre_min using min-heap.
        pre_min, cur_min = [sum(A[:n])], sum(A[:n])
        pre_hp = [-x for x in A[:n]]
        heapq.heapify(pre_hp)
        for i in range(n, 2 * n):
            cur_pop = -heapq.heappop(pre_hp)
            cur_min -= cur_pop
            cur_min += min(cur_pop, A[i])
            pre_min.append(cur_min)
            heapq.heappush(pre_hp, -min(cur_pop, A[i]))          
        
        # Build suf_max.
        suf_max, cur_max = [sum(A[2*n:])], sum(A[2*n:])
        suf_hp = [x for x in A[2*n:]]
        heapq.heapify(suf_hp)        
        for i in range(2 * n - 1, n - 1, -1):
            cur_pop = heapq.heappop(suf_hp)
            cur_max -= cur_pop
            cur_max += max(cur_pop, A[i])
            suf_max.append(cur_max)
            heapq.heappush(suf_hp, max(cur_pop, A[i]))
        suf_max = suf_max[::-1]
        
        # Iterate over pre_min and suf_max and get the minimum difference.
        ans = math.inf
        for a, b in zip(pre_min, suf_max):
            ans = min(ans, a - b)
        return ans 
        
# class Solution:
#     def minimumDifference(self, nums: List[int]) -> int:
#         N = len(nums)//3 
        
#         left_part = [-n for n in nums[0:N]]
#         right_part = nums[-N:]
#         heapq.heapify(left_part)
#         heapq.heapify(right_part)
        
#         min_left_part = [0] * (N+1)
#         max_right_part = [0] * (N+1)
#         min_left_part[0] = -sum(left_part)
#         max_right_part[-1] = sum(right_part)

#         for i in range(1, N+1):
#             n = nums[N+i-1]
#             heapq.heappush(left_part, -n)
#             pn = -heapq.heappop(left_part)
#             min_left_part[i] = min_left_part[i-1] + (n - pn)
            
#         for i in range(N-1, -1, -1):
#             n = nums[N+i]
#             heapq.heappush(right_part, n)
#             pn = heapq.heappop(right_part)
#             max_right_part[i] = max_right_part[i+1] + (n - pn)
        
#         ans = inf
#         for i in range(N+1):
#             ans = min(ans, min_left_part[i] - max_right_part[i])
#         return ans
1431