#K8446. Maximum Magical Effect Subset

    ID: 36424 Type: Default 1000ms 256MiB

Maximum Magical Effect Subset

Maximum Magical Effect Subset

You are given (N) bottles, each with a magical effect represented by an integer. Your task is to choose exactly (K) bottles such that the combined magical effect (i.e. the sum of the (K) chosen integers) is maximized. In case there are multiple subsets that yield the same maximum sum, you must choose the subset which, when sorted in ascending order, is lexicographically smallest.

For example, if (N=5), (K=3) and the bottle effects are [1, 3, -2, 5, 4], then the best subset is [3, 4, 5] because:

  1. The sum (3+4+5=12) is the maximum possible.
  2. The subset [3, 4, 5] is already sorted and is lexicographically smaller than any other subset with sum 12.

Solve the problem using efficient sorting and selection techniques.

The mathematical expression for the combined magical effect is given by: [ \text{max_sum} = \sum_{i=1}^{K} a_i, ] where (a_i) are the chosen bottle effects.

inputFormat

The input is given via standard input (stdin). The first line contains two integers (N) and (K), where (N) is the total number of bottles and (K) is the number of bottles you must choose. The second line contains (N) space-separated integers representing the magical effects of each bottle.

outputFormat

Output the selected (K) bottle effects in ascending order, separated by a single space, via standard output (stdout).## sample

5 3
1 3 -2 5 4
3 4 5

</p>