#K54712. Minimum Sum of Card Values
Minimum Sum of Card Values
Minimum Sum of Card Values
You are given a collection of cards with integer values. Each player is dealt n cards. Your task is to pick exactly k cards such that the sum of their values is minimized. Formally, if the list of card values is \(a_1, a_2, \dots, a_n\), then you need to compute the minimum sum \(S = \sum_{i=1}^{k} a_{(i)}\), where \(a_{(1)} \leq a_{(2)} \leq \cdots \leq a_{(n)}\) are the card values in non-decreasing order.
Input: The first line contains two integers n and k. The second line contains n integers representing the values of the cards.
Output: Print a single integer representing the minimum possible sum of values by picking exactly k cards.
For example, if the input is:
5 3 7 2 3 8 9
After sorting the card values as \(2, 3, 7, 8, 9\), the sum of the smallest 3 cards is \(2 + 3 + 7 = 12\), so the output should be 12
.
inputFormat
The input is given via the standard input (stdin). The first line contains two space-separated integers n and k:
- n: the number of cards.
- k: the number of cards to choose.
The second line contains n space-separated integers, each representing the value of a card.
outputFormat
Print a single integer to the standard output (stdout) representing the minimum possible sum of the values of k cards.
## sample5 3
7 2 3 8 9
12
</p>