#K56957. Closest Sum of Top K Cards

    ID: 30313 Type: Default 1000ms 256MiB

Closest Sum of Top K Cards

Closest Sum of Top K Cards

In this problem, you are given a deck of N cards, each having a value between 1 and M. You are also provided with two additional numbers: K and T. Although T appears in the input, the challenge is to rearrange the deck such that when you take the top K cards, their sum is as high as possible according to the following strategy:

Sort all the cards in non‐increasing order and select the first K cards. Then, compute the sum of these K cards.

Mathematically, if the card values are given by \(a_1, a_2, \dots, a_N\), after sorting them in descending order to obtain \(b_1 \geq b_2 \geq \dots \geq b_N\), your task is to compute:

\[ S = \sum_{i=1}^{K} b_i \]

This sum \(S\) is then output. Note that even though a target sum \(T\) is provided in the input, it does not affect the selection of cards in this problem.

inputFormat

The input is given via standard input and consists of:

  • Four integers separated by spaces: N (number of cards), M (maximum card value), K (number of top cards to sum), and T (target sum, which is not used in the calculation).
  • N integers representing the values on the cards.

For example:

5 10 3 15 8 2 9 7 4

outputFormat

Output a single integer via standard output: the sum of the top K cards after sorting the deck in non‐increasing order.

For the sample input above, the output would be:

24
## sample
5 10 3 15 8 2 9 7 4
24