#C9412. Maximum Magical Potency

    ID: 53503 Type: Default 1000ms 256MiB

Maximum Magical Potency

Maximum Magical Potency

You are given multiple test cases. For each test case, you have N ingredients, each with a magical potency value. You are allowed to use at most K ingredients. Your task is to select up to K ingredients such that the sum of their magical potencies is maximized.

For each test case, you will be provided:

  • N: the number of available ingredients
  • K: the maximum number of ingredients you can use
  • A list of N integers representing the magical potency scores of the ingredients

You can model the problem by selecting the top K scores. In mathematical notation, if the sorted scores (in descending order) for a test case are \(a_1, a_2, \ldots, a_N\), then the answer is given by:

[ \text{Answer} = \sum_{i=1}^{K} a_i ]

Note: It is guaranteed that the number of ingredients provided is at least K in each test case.

Example:

Input:
2
5 3
2 5 7 8 10
4 2
1 4 6 3

Output: 25 10

</p>

inputFormat

The input is read from stdin and has the following format:

T
N1 K1
a11 a12 ... a1N1
N2 K2
a21 a22 ... a2N2
... 
NT KT
aT1 aT2 ... aTNT

Where:

  • T is the number of test cases.
  • For each test case, the first line contains two integers N and K.
  • The second line of each test case contains N space-separated integers, each representing the magical potency score of an ingredient.

outputFormat

For each test case, output a single line on stdout representing the maximum sum of magical potencies obtainable by selecting at most K ingredients.

Example Output:

25
10
## sample
2
5 3
2 5 7 8 10
4 2
1 4 6 3
25

10

</p>