#K43387. Maximum Cumulative Ore Yield
Maximum Cumulative Ore Yield
Maximum Cumulative Ore Yield
You are given multiple test cases. In each test case, there are two integers \(N\) and \(K\), followed by an array of \(N\) integers representing the ore yields of different mining zones. The task is to determine the maximum possible cumulative ore yield after placing exactly \(K\) stabilization devices. To maximize the yield, you should install the devices in the zones with the highest yields.
Formally, if the ore yields are given by the array \(A = [a_1, a_2, \dots, a_N]\), sort \(A\) in descending order as \(a_{\sigma(1)} \ge a_{\sigma(2)} \ge \dots \ge a_{\sigma(N)}\). Then, the answer is:
\[ S = \sum_{i=1}^{K} a_{\sigma(i)} \]If \(K > N\), simply sum up all the yields.
inputFormat
The first line of input contains a single integer (T) ((1 \le T \le 100)), which represents the number of test cases. Each test case consists of two lines. The first line contains two integers (N) and (K) ((0 \le N \le 10^5), (0 \le K \le N)). The second line contains (N) space-separated integers representing the ore yields of the mining zones.
outputFormat
For each test case, print a single line containing the maximum cumulative ore yield after optimally placing the stabilization devices.## sample
4
5 2
10 20 30 40 50
6 3
1 5 3 2 8 7
1 1
100
4 2
50 100 200 150
90
20
100
350
</p>