#K46767. Maximum Total Potion Effect
Maximum Total Potion Effect
Maximum Total Potion Effect
You are given several test cases. In each test case, there are N potions each having an effect strength and you need to choose exactly K potions such that their total effect strength is maximized.
Formally, for each test case you are given two integers \(N\) and \(K\) and a list of \(N\) integers \(P_1, P_2, \dots, P_N\). Your task is to compute the sum of the largest \(K\) numbers from the list. It is guaranteed that \(1 \leq K \leq N\).
Input Format: The first line of input contains an integer \(T\) representing the number of test cases. For each test case, the first line contains two integers \(N\) and \(K\). The second line contains \(N\) space-separated integers representing the effect strengths.
Output Format: For each test case, output a single line containing the maximum total effect strength.
Make sure your program reads from stdin
and writes to stdout
.
inputFormat
The input begins with a single integer \(T\) representing the number of test cases. Each test case is described as follows:
- A line with two space-separated integers \(N\) and \(K\);
- A line with \(N\) space-separated integers representing the effect strengths of the potions.
All input should be read from stdin
.
outputFormat
For each test case, output the maximum total effect strength obtained by summing the largest \(K\) numbers from the list. Each result should be printed on a new line. Write all output to stdout
.
3
5 3
1 2 3 4 5
6 2
5 8 7 1 6 2
4 4
1 1 1 1
12
15
4
</p>