<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You want to find a <strong>subsequence </strong>of <code>nums</code> of length <code>k</code> that has the <strong>largest</strong> sum.</p>

<p>Return<em> </em><em><strong>any</strong> such subsequence as an integer array of length </em><code>k</code>.</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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,3,3], k = 2
<strong>Output:</strong> [3,3]
<strong>Explanation:</strong>
The subsequence has the largest sum of 3 + 3 = 6.</pre>

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

<pre>
<strong>Input:</strong> nums = [-1,-2,3,4], k = 3
<strong>Output:</strong> [-1,3,4]
<strong>Explanation:</strong> 
The subsequence has the largest sum of -1 + 3 + 4 = 6.
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,4,3,3], k = 2
<strong>Output:</strong> [3,4]
<strong>Explanation:</strong>
The subsequence has the largest sum of 3 + 4 = 7. 
Another possible subsequence is [4, 3].
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>-10<sup>5</sup>&nbsp;&lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

class Solution:
    def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
        arr = [x for x in nums]
        n = len(nums)
        def quickselect(l, r) :
            if l == r :
                return 
            pivot = arr[r]
            i = l
            j = l
            while i < r :
                if arr[i] >= pivot :
                    arr[i] , arr[j] = arr[j], arr[i]
                    j += 1
                i += 1
            arr[j] , arr[r] = arr[r], arr[j]
            if j == k-1 : return 
            elif j < k-1 : 
                quickselect(j+1, r)
                return
            elif j > k-1 : 
                quickselect(l, j-1)
            return 
        
        quickselect(0, n-1)

        kth_val = arr[k-1]
        print(arr[k-1])
        print(arr)
        res = []

        count = arr[:k].count(kth_val)  # Count how many times kth_val appears in the first k elements
        
        for x in nums:
            if x > kth_val:  # Add elements larger than kth_val
                res.append(x)
            elif x == kth_val and count > 0:  # Add kth_val but only `count` times
                res.append(x)
                count -= 1
            if len(res) == k:
                break
        return res











48