#C8472. Maximum Subsequence Sum
Maximum Subsequence Sum
Maximum Subsequence Sum
You are given a list of integers and an integer \(k\). Your task is to select exactly \(k\) elements from the list (not necessarily contiguous) such that their sum is maximized.
In formal terms, given an array \(nums\) of \(n\) integers and an integer \(k\), find the maximum sum \(\sum_{i=1}^{k} a_i\) where \(a_i\) are the selected elements from \(nums\). Note that \(k\) can be any integer between \(1\) and \(n\), inclusive. If \(k = n\), the answer is simply the sum of all elements in \(nums\).
The problem requires efficient reading of inputs from stdin
and writing the answer to stdout
.
inputFormat
The first line contains two integers \(n\) and \(k\) separated by a space, where \(n\) represents the number of elements in the list and \(k\) is the length of the subsequence.
The second line contains \(n\) integers separated by spaces, representing the elements of the list \(nums\).
outputFormat
Output a single integer, the maximum possible sum that can be obtained by selecting exactly \(k\) elements from \(nums\).
## sample5 3
1 2 3 4 5
12
</p>