#K42932. Sum of k Smallest Numbers
Sum of k Smallest Numbers
Sum of k Smallest Numbers
You are given a list of ( n ) integers and an integer ( k ). The task is to compute the sum of the first ( k ) smallest integers in the list. In other words, after sorting the array in non-decreasing order, you need to calculate
[ S = \sum_{i=1}^{k} A_i, ]
where ( A_i ) represents the ( i^{th} ) smallest element of the list. It is guaranteed that ( k ) is at most ( n ).
inputFormat
The input is given via standard input (stdin) and contains two lines:
-
The first line contains two space-separated integers ( n ) (the length of the list) and ( k ) (the number of smallest elements to sum).
-
The second line contains ( n ) space-separated integers representing the list.
outputFormat
Output a single integer that is the sum of the first ( k ) smallest numbers in the list. The result should be printed to standard output (stdout).## sample
6 3
3 1 5 9 2 8
6