#K83577. Max Achievable Score

    ID: 36228 Type: Default 1000ms 256MiB

Max Achievable Score

Max Achievable Score

You are given a list of N integers, and an integer X. Your task is to select exactly X elements from the given list such that their sum is maximized. In other words, you need to calculate the maximum achievable score by picking the X largest elements.

The problem can be formally stated as follows:

Given a list \(m = [m_1, m_2, \dots, m_N]\) and an integer \(X\), compute:

[ \text{score} = \sum_{i=1}^{X} m_{(i)} ]

where \(m_{(i)}\) denotes the \(i\)-th largest element in the list.

You need to read the input from standard input and output the result to standard output.

inputFormat

The first line of input contains two space-separated integers N and X, where N is the number of elements in the list and X is the number of elements to choose.

The second line contains N space-separated integers representing the list m.

Input Format:

N X
m[0] m[1] ... m[N-1]

outputFormat

Output a single integer representing the maximum achievable score by selecting exactly X elements.

Output Format:

result

In mathematical notation, if \(m_{(1)}, m_{(2)}, \dots, m_{(X)}\) are the X largest elements, then:

[ \text{result} = \sum_{i=1}^{X} m_{(i)} ]

## sample</p>
7 3
6 9 5 3 8 9 4
26