<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>. You can choose any <strong>subsequence</strong> of the array and sum all of its elements together.</p>

<p>We define the <strong>K-Sum</strong> of the array as the <code>k<sup>th</sup></code> <strong>largest</strong> subsequence sum that can be obtained (<strong>not</strong> necessarily distinct).</p>

<p>Return <em>the K-Sum of the array</em>.</p>

<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>

<p><strong>Note</strong> that the empty subsequence is considered to have a sum of <code>0</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [2,4,-2], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> All the possible subsequence sums that we can obtain are the following sorted in decreasing order:
- 6, 4, 4, 2, <u>2</u>, 0, 0, -2.
The 5-Sum of the array is 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,-2,3,4,-10,12], k = 16
<strong>Output:</strong> 10
<strong>Explanation:</strong> The 16-Sum of the array is 10.
</pre>

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

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= min(2000, 2<sup>n</sup>)</code></li>
</ul>

# from sortedcontainers import SortedList

# class Solution:
#     def kSum(self, nums: List[int], k: int) -> int:
#         nums.sort(reverse=True)
#         if nums[k-1] > 0:

#         lst = SortedList([0])
#         my_max = 0

#         for num in nums:
#             if num > 0:
#                 my_max += num
#             elif my_max + num <= lst[0] and len(lst) == k:
#                 continue

#             new_lst = lst.copy()
#             while len(lst) < k and new_lst:
#                 lst.add(new_lst.pop() + num)
            
#             while new_lst and new_lst[-1] + num > lst[0]:
#                 lst.pop(0)
#                 lst.add(new_lst.pop() + num)

#             # new_heap = [x + num for x in heap]
#             # if num > 0:
#             #     heap, new_heap = new_heap, heap

#             # while len(heap) < k and new_heap:
#             #     heapq.heappush(heap, new_heap.pop())

#             # new_heap = sorted(filter(lambda x: x > heap[0], new_heap))
#             # for x in reversed(new_heap):
#             #     if x > heap[0]:
#             #         heapq.heapreplace(heap, x)
#             #     else:
#             #         break

#         return lst[0]

import heapq

class Solution:
    def kSum(self, nums: List[int], k: int) -> int:
        result = sum((num for num in nums if num > 0))
        nums = sorted((num if num <= 0 else -num for num in nums), reverse=True)
        heap = [0]
        my_max = 0

        for num in nums:
            if num > 0:
                my_max += num
            elif my_max + num <= heap[0] and len(heap) == k:
                continue

            new_heap = [x + num for x in heap]
            if num > 0:
                heap, new_heap = new_heap, heap

            while len(heap) < k and new_heap:
                heapq.heappush(heap, new_heap.pop())

            new_heap = sorted(filter(lambda x: x > heap[0], new_heap))
            for x in reversed(new_heap):
                if x > heap[0]:
                    heapq.heapreplace(heap, x)
                else:
                    break

        return result + heap[0]
959