#K94972. Maximum Subsequence Sum
Maximum Subsequence Sum
Maximum Subsequence Sum
You are given a sequence of n integers and an integer k. Your task is to select k integers from the sequence such that their sum is maximized. Note that the selected integers do not need to be contiguous in the original sequence.
Mathematically, you need to compute:
$$\max_{S \subseteq A, |S|=k} \sum_{x \in S} x$$
A useful hint is to sort the sequence in non-increasing order and sum up the first k numbers to obtain the maximum possible sum.
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains two space-separated integers n and k, where n is the length of the sequence and k is the number of integers to select.
- The second line contains n space-separated integers representing the sequence.
outputFormat
Output a single integer via stdout which is the maximum sum obtainable by selecting k integers from the sequence.
## sample5 3
2 1 5 1 3
10