#C14427. Sum of K Largest Numbers
Sum of K Largest Numbers
Sum of K Largest Numbers
You are given a list of integers and an integer k. Your task is to compute the sum of the k largest numbers in the list. If k is greater than or equal to the number of elements in the list, simply output the sum of all the numbers.
The problem can be formally described as follows:
Given a list of integers \(A = [a_1, a_2, \dots, a_n]\) and an integer \(k\), find \(S\) where \[ S = \begin{cases} \sum_{i=1}^{k} b_i, & \text{if } k < n\\ \sum_{i=1}^{n} a_i, & \text{if } k \ge n \end{cases} \] and \(b_1, b_2, \dots, b_k\) are the k largest elements in \(A\).
Input will be provided via stdin and output should be printed to stdout.
inputFormat
The first line contains two space-separated integers \(n\) and \(k\), where \(n\) is the number of integers in the list and \(k\) is the number of largest elements to sum. The second line contains \(n\) space-separated integers representing the list \(A\).
outputFormat
Output a single integer which is the sum of the k largest numbers in the list, following the rule that if \(k \ge n\), output the sum of all the numbers.
## sample6 3
4 1 7 3 8 5
20