#C4394. Maximum Sum Subsequence
Maximum Sum Subsequence
Maximum Sum Subsequence
You are given t test cases. For each test case, you are provided with an array of n integers and an integer k. Your task is to select exactly k elements from the array such that their sum is maximized.
In other words, if you denote the array by \(a_1, a_2, \dots, a_n\), you need to compute \[ S = \max \Big\{ a_{i_1} + a_{i_2} + \cdots + a_{i_k} \;:\; 1 \le i_1 < i_2 < \cdots < i_k \le n \Big\} \] by selecting the k largest numbers in the array.
Note that the array can include negative numbers. The strategy is to simply sort the array in descending order and sum up the first k numbers.
inputFormat
The first line contains a single integer t representing the number of test cases. Each test case is described as follows:
- The first line of each test case contains two integers n and k where n is the number of elements in the array and k is the length of the subsequence to choose.
- The second line contains n space-separated integers representing the elements of the array.
Assume that \(1 \le k \le n\) for every test case.
outputFormat
For each test case, output a single line containing one integer, the maximum possible sum of a subsequence of length k.
## sample5
5 3
1 2 3 4 5
6 2
-5 -1 3 1 2 9
4 4
-10 -5 -1 0
5 3
-5 -1 -3 -4 -2
6 4
-10 -5 5 0 20 15
12
12
-16
-6
40
</p>