<p>You are given two positive <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>

<p>The <strong>sum of squared difference</strong> of arrays <code>nums1</code> and <code>nums2</code> is defined as the <strong>sum</strong> of <code>(nums1[i] - nums2[i])<sup>2</sup></code> for each <code>0 &lt;= i &lt; n</code>.</p>

<p>You are also given two positive integers <code>k1</code> and <code>k2</code>. You can modify any of the elements of <code>nums1</code> by <code>+1</code> or <code>-1</code> at most <code>k1</code> times. Similarly, you can modify any of the elements of <code>nums2</code> by <code>+1</code> or <code>-1</code> at most <code>k2</code> times.</p>

<p>Return <em>the minimum <strong>sum of squared difference</strong> after modifying array </em><code>nums1</code><em> at most </em><code>k1</code><em> times and modifying array </em><code>nums2</code><em> at most </em><code>k2</code><em> times</em>.</p>

<p><strong>Note</strong>: You are allowed to modify the array elements to become <strong>negative</strong> integers.</p>

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

<pre>
<strong>Input:</strong> nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0
<strong>Output:</strong> 579
<strong>Explanation:</strong> The elements in nums1 and nums2 cannot be modified because k1 = 0 and k2 = 0. 
The sum of square difference will be: (1 - 2)<sup>2 </sup>+ (2 - 10)<sup>2 </sup>+ (3 - 20)<sup>2 </sup>+ (4 - 19)<sup>2</sup>&nbsp;= 579.
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1
<strong>Output:</strong> 43
<strong>Explanation:</strong> One way to obtain the minimum sum of square difference is: 
- Increase nums1[0] once.
- Increase nums2[2] once.
The minimum of the sum of square difference will be: 
(2 - 5)<sup>2 </sup>+ (4 - 8)<sup>2 </sup>+ (10 - 7)<sup>2 </sup>+ (12 - 9)<sup>2</sup>&nbsp;= 43.
Note that, there are other ways to obtain the minimum of the sum of square difference, but there is no way to obtain a sum smaller than 43.</pre>

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

<ul>
	<li><code>n == nums1.length == nums2.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= k1, k2 &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minSumSquareDiff(self, nums1: List[int], nums2: List[int], k1: int, k2: int) -> int:

        K = k1 + k2
        diffs = [
            abs(A - B)
            for (A, B) in zip(nums1, nums2)
        ]
        diffCounts = Counter(diffs)
        diffGroups = list(diffCounts.items())
        diffGroups.sort()
        # print(f'{K}: {diffGroups[-5:]=}')

        while K:
            (largest, Count) = diffGroups.pop(-1)
            if diffGroups:
                (second, Count2) = diffGroups[-1]
                if second == largest:
                    ignore = diffGroups.pop(-1)
                    Count += Count2
                    print(f'  Merge last 2 items: {Count=}')
                    changeSize = 1
                    print(f'A: {changeSize=}')
                elif K >= (Count * (largest - second)):
                    changeSize = (largest - second)
                    print(f'B: {changeSize=}')
                elif K >= Count:
                    # should merge this one and the succeeding
                    changeSize = 1
                    print(f'C: {changeSize=}')
                else:
                    changeSize = 1
                    print(f'D: {changeSize=}')
            elif K >= largest * Count:
                changeSize = largest
                print(f'E: {changeSize=}')
            else:
                changeSize = max(1, K // Count)
                print(f'F: {changeSize=}')
            if largest == 0:
                # put the zero record back in
                bisect.insort(diffGroups, (largest, Count))
                break
            if K >= Count * changeSize:
                print(f'  Delete {changeSize} from group of {Count}')
                largest -= changeSize
                K -= (Count * changeSize)
                bisect.insort(diffGroups, (largest, Count))
            elif K >= Count:
                # Should not be necessary, if changeSize is being set correctly
                print(f'  Delete {1} from group of {Count}')
                largest -= 1
                K -= (Count * 1)
                bisect.insort(diffGroups, (largest, Count))
            else:
                print(f'  Not enough K left for {Count}-size group:')
                print(f'    A: Split off group of {Count - K}')
                bisect.insort(diffGroups, (largest, Count - K))
                largest -= 1
                print(f'    B: Delete 1 from group of {K}')
                bisect.insort(diffGroups, (largest, K))
                K -= K

            # print(f'{K}: {diffGroups[-5:]=}')
        
        Square = lambda x: (x * x)

        newDiffs = [
            Diff
            for (Diff, Count) in diffGroups
            for i in range(Count)
        ]
        print(f'{newDiffs=}')

        squares = list(map(Square, newDiffs))
        print(f'{squares=}')
        
        return sum(squares)

919