You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum.

Return any such subsequence as an integer array of length k.

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

&nbsp;
Example 1:

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

Example 2:

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


Example 3:

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


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 1000
	-105&nbsp;&lt;= nums[i] &lt;= 105
	1 &lt;= k &lt;= nums.length


class Solution(object):
    def maxSubsequence(self, nums, k):
        ret, max_k = [], sorted(nums, reverse=True)[:k]
        for num in nums:
            if num in max_k:
                ret.append(num)
                max_k.remove(num)
                if len(max_k) == 0:
                    return ret
class Solution {
    public int[] maxSubsequence(int[] nums, int k) {
        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> b[0] - a[0]);
        
        for(int i=0; i<nums.length; i++)
            pq.offer(new int[]{nums[i], i});
        
        List<int[]> l = new ArrayList<>();
        
        while(k-- != 0)
            l.add(pq.poll());
        
        Collections.sort(l, (a,b) -> a[1] - b[1]);
            
        int[] res = new int[l.size()];
        
        int index = 0;
        
        for(int[] i: l)
            res[index++] = i[0];
        
        return res;
    }
}
// OJ: https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
    vector<int> maxSubsequence(vector<int>& A, int k) {
        vector<int> id(A.size());
        iota(begin(id), end(id), 0); // Index array 0, 1, 2, ...
        sort(begin(id), end(id), [&](int a, int b) { return A[a] > A[b]; }); // Sort the indexes in descending order of their corresponding values in `A`
        id.resize(k); // Only keep the first `k` indexes with the greatest `A` values
        sort(begin(id), end(id)); // Sort indexes in ascending order
        vector<int> ans;
        for (int i : id) ans.push_back(A[i]);
        return ans;
    }
};
var maxSubsequence = function(nums, k) {
    return nums.map((v,i)=>[v,i]).sort((a,b)=>a[0]-b[0]).slice(-k).sort((a,b)=>a[1]-b[1]).map(x=>x[0]);
};
Find Subsequence of Length K With the Largest Sum