#K58932. Maximum Banana Sweetness

    ID: 30752 Type: Default 1000ms 256MiB

Maximum Banana Sweetness

Maximum Banana Sweetness

You are given n bananas, each with a specified sweetness value, and an integer k. Monkey Kong is required to eat exactly k bananas. Your task is to choose k bananas in such a way that the total sweetness is maximized. Note that even if some bananas have negative sweetness values, Monkey Kong still must eat exactly k bananas.

Problem Details:

  • The sweetness values of the bananas are provided in an array.
  • You need to compute the sum of the k largest sweetness values.

Mathematical Formulation:

Let \(a_1, a_2, \dots, a_n\) be the sweetness values. Then the answer is:

\[ \sum_{i=1}^{k} b_i, \quad\text{where } b_1 \ge b_2 \ge \cdots \ge b_n \text{ is a permutation of } a_1, a_2, \dots, a_n. \]

inputFormat

The input is read from stdin and consists of two lines:

  • The first line contains two space-separated integers n and k, where 1 ≤ k ≤ n.
  • The second line contains n space-separated integers representing the sweetness values of the bananas.

For example:

5 3
2 -5 6 -3 8

outputFormat

Output a single integer to stdout that represents the maximum total sweetness obtained by selecting exactly k bananas.

For the sample input above, the output should be:

16
## sample
5 3
2 -5 6 -3 8
16