<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>

import heapq
class Solution:
    # Method: heap -> T = O(nlogn)
    # Because it's possible to have both pos and neg in the arr,
    # The k largest sum of subseq = sum(larget_subseq) - sum((k - 1)th smallest subseq)
    # sum(largest_subseq) = sum(all positive numbers in original array)
    def kSum(self, nums: List[int], k: int) -> int:
        n = len(nums)

        # step_1: get the max_sum of sub-seq 
        a = sum(x for x in nums if x > 0)

        # step_2: get the (k - 1)th smallest sum among all sub-seq 
        arr = [abs(x) for x in nums] 
        b = self.get_kth_minSum(arr, k - 1)

        # step_3: output
        return a - b 

    def get_kth_minSum(self, A, k):
        A.sort() 

        heap = [] 
        heapq.heappush(heap, (0, 0))  # cur_sum, cur_index

        for _ in range(k):
            cur_sum, cur_idx = heapq.heappop(heap)

            # case_1: next_sum = cur_sum + next_number 
            if cur_idx in range(len(A)):
                next_sum = cur_sum + A[cur_idx]
                next_idx = cur_idx + 1
                heapq.heappush(heap, (next_sum, next_idx))
                
                # case_2: next_sum = cur_sum + next_number - prev_number 
                if cur_idx > 0:
                    next_sum_v2 = next_sum - A[cur_idx - 1]
                    heapq.heappush(heap, (next_sum_v2, next_idx))
        
        return heap[0][0]






    

547