#K51272. Most Beautiful Combination
Most Beautiful Combination
Most Beautiful Combination
You are given n types of cakes, each with a decoration value, and an integer m representing the number of cakes you need to select. Your task is to choose m cakes such that the sum of their decoration values is maximized.
Mathematically, given decoration values \(d_1, d_2, \dots, d_n\), you need to select a subset \(S\) of \(m\) elements such that \[ \sum_{i \in S} d_i \] is maximized. This optimal subset is known as the most beautiful combination.
Note: If there are multiple valid answers, any answer with the maximum sum is acceptable. In our implementation, we choose to output the selected decoration values in descending order.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains two space-separated integers
n
andm
, wheren
is the number of cake types andm
is the number of cakes to select. - The second line contains
n
space-separated integers representing the decoration values of the cakes.
outputFormat
Output a single line to standard output (stdout) containing m
space-separated integers — the decoration values of the selected cakes in descending order.
5 3
1 3 5 7 9
9 7 5