#K9286. Maximum Subsequence Sum
Maximum Subsequence Sum
Maximum Subsequence Sum
You are given an array of n integers and an integer k. Your task is to select any subsequence of k elements from the array such that the sum of the selected elements is maximized.
Formally, if the array is \(A = [a_1, a_2, \dots, a_n]\), you need to choose a set of indices \(\{i_1, i_2, \dots, i_k\}\) (where \(1 \le i_j \le n\)) so that the sum \(a_{i_1} + a_{i_2} + \cdots + a_{i_k}\) is as large as possible. It is guaranteed that \(k \le n\).
The answer should be printed to standard output.
inputFormat
The first line contains two integers n and k separated by a space.
The second line contains n integers separated by spaces, representing the elements of the array.
outputFormat
Output a single integer, which is the maximum sum that can be obtained by choosing any subsequence of k elements.
## sample6 2
3 1 -2 4 6 -1
10