#C11867. Maximum Operational Hours for Batteries
Maximum Operational Hours for Batteries
Maximum Operational Hours for Batteries
You are given N batteries with their respective lifespans (in hours). Your task is to choose exactly K batteries such that the sum of their lifespans is maximized.
The batteries are used as a group in a device that requires exactly K batteries to operate. In mathematical terms, if you pick the battery lifespans \(a_1, a_2, \dots, a_K\) (after sorting in descending order), then the maximum operational hours is given by:
$$\text{Operational Hours} = \sum_{i=1}^{K} a_i$$
Read the input, compute the desired sum, and print the result.
inputFormat
The first line contains two integers N and K, where N is the number of batteries and K is the number of batteries to be used.
The second line contains N space-separated integers representing the lifespans of the batteries.
outputFormat
Output a single integer, which is the maximum operational hours that can be obtained by using exactly K batteries.
## sample6 3
10 20 30 40 50 60
150
</p>