<p>You are given an integer array <code>nums</code> of <code>2 * n</code> integers. You need to partition <code>nums</code> into <strong>two</strong> arrays of length <code>n</code> to <strong>minimize the absolute difference</strong> of the <strong>sums</strong> of the arrays. To partition <code>nums</code>, put each element of <code>nums</code> into <strong>one</strong> of the two arrays.</p>

<p>Return <em>the <strong>minimum</strong> possible absolute difference</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/02/ex1.png" style="width: 240px; height: 106px;" />
<pre>
<strong>Input:</strong> nums = [3,9,7,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> One optimal partition is: [3,9] and [7,3].
The absolute difference between the sums of the arrays is abs((3 + 9) - (7 + 3)) = 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [-36,36]
<strong>Output:</strong> 72
<strong>Explanation:</strong> One optimal partition is: [-36] and [36].
The absolute difference between the sums of the arrays is abs((-36) - (36)) = 72.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="example-3" src="https://assets.leetcode.com/uploads/2021/10/02/ex3.png" style="width: 316px; height: 106px;" />
<pre>
<strong>Input:</strong> nums = [2,-1,0,4,-2,-9]
<strong>Output:</strong> 0
<strong>Explanation:</strong> One optimal partition is: [2,4,-9] and [-1,0,-2].
The absolute difference between the sums of the arrays is abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0.
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 15</code></li>
	<li><code>nums.length == 2 * n</code></li>
	<li><code>-10<sup>7</sup> &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
</ul>

class Solution:
    def minimumDifference(self, nums: List[int]) -> int:
        # split into two arrays of size n
        
        # ideally we get a abs diff of zero with sum//2 and sum//2
        # otherwise we end with sum//2+d and sum//2-d

        # we want the smallest difference d

        # wlog one subset will have a higher sum than the other, that might help us

        # so if we can find the highest sum of N//2 elements <= T//2
        # then that's our answer

        # if we sort asc. then we'll get to a point after several choices, hopefully, where we conclude that we're
        # already above T//2 and should thus give up 

        # but the challenge is that if we look at all combinations thats 30 choose 15 worst case which is way too many, over 1e8

        # choosing up to half the sum should help

        # nums.sort()
        # acc = nums[:]
        # for i in range(N-2, -1, -1): acc[i] += acc[i+1]
        # acc.append(0)

        # T = acc[0]//2

        # most = -math.inf
        
        # def dfs(i: int, r: int, s: int) -> None:
        #     nonlocal most

        #     if r == 0:
        #         most = max(most, s)
        #         return

        #     if N-i == r:
        #         if s + acc[i] <= T: most = max(most, s+acc[i])
        #         return

        #     # lowest sum we can get is s + next_r
        #     # highest sum is s + last_r
        #     lowest_r = acc[i] - acc[i+r]
        #     highest_r = acc[N-r]

        #     if s + highest_r <= most or s + lowest_r > T: return
        
        #     dfs(i+1, r, s)
        #     dfs(i+1, r-1, s+nums[i])

        # dfs(0, N//2, 0)

        # return acc[0]-2*most # larger half has T-most, smaller is most, diff is T-most-most

        # faster but still not good enough

        # what if we did
        #   combinations where we took 0..N from left side: 2**N = 2**15, feasible
        #   combinations where we took 0..N from right side: 2**N = 2**15 again

        # then for each l in 0..8 we iterated to find the max
        # e.g. sort within each, iterate over one, and increase other while we can and increase is still <= T

        N = len(nums)//2
        T = sum(nums)
        half = T//2
        lefts = [set() for _ in range(N+1)]
        rights = [set() for _ in range(N+1)]
        lefts[0] = {0}
        rights[0] = {0}
        for i in range(N):
            l = nums[i]
            r = nums[-i-1]

            for count in range(N, 0, -1):
                lefts[count].update(s + l for s in lefts[count-1])
                rights[count].update(s + r for s in rights[count-1])

        for count in range(N+1):
            lefts[count] = list(lefts[count])
            lefts[count].sort()

            rights[count] = list(rights[count])
            rights[count].sort()

        # print(f"{nums=}")

        most = -math.inf
        for lc in range(N+1):
            rc = N - lc

            # print(f"{lc=}")

            # as we ascend in lefts[lc], we need ever lower value in rights[rc]
            j = len(rights[rc])-1
            for ls in lefts[lc]:
                while j >= 0 and ls + rights[rc][j] > half: j -= 1
                if j == -1: 
                    # print(f"  {ls=}, {j=}. {ls+rights[rc][0]=}, {half=}")
                    break # no j in right sums to make a value <= T
                most = max(most, ls+rights[rc][j])
                # print(f"  {ls=}, {rights[rc][j]=}, {most=}")
                if most == half: break

        return T - 2*most # larger - smaller == T-most - most
24300