#K43927. Maximum Subsequence Sum

    ID: 27418 Type: Default 1000ms 256MiB

Maximum Subsequence Sum

Maximum Subsequence Sum

You are given t test cases. For each test case, you are provided with an integer array of length n and an integer k. Your task is to choose exactly k elements from the array such that the sum of the selected elements is maximized. In other words, you need to find the maximum possible sum by selecting a subsequence of k numbers from the array.

The problem can be formalized as follows:

Given an array \(A = [a_1, a_2, \dots, a_n]\) and an integer \(k\), maximize \(\sum_{i \in S} a_i\) subject to \(|S| = k\), where \(S\) is a subset of indices of the array.

Note: The subsequence does not have to be contiguous.

inputFormat

The first line of input contains an integer t, the number of test cases. Each test case is described as follows:

  • The first line of each test case contains two space-separated integers n and k.
  • The second line contains n space-separated integers representing the array elements.

outputFormat

For each test case, output a single line containing the maximum possible sum of a subsequence of length k.

## sample
3
5 3
1 2 3 4 5
6 2
-1 3 -2 4 -5 6
4 4
-1 -2 -3 -4
12

10 -10

</p>