#K65537. Maximum Coins Collection

    ID: 32219 Type: Default 1000ms 256MiB

Maximum Coins Collection

Maximum Coins Collection

You are given n stacks of coins represented by a list of non-negative integers and an integer k. In one operation, you can pick one coin from any stack. Your task is to perform exactly k operations such that you maximize the total number of coins collected. Essentially, you should always choose the available coin with the highest value.

Formally, let \(P = [p_1, p_2, \ldots, p_n]\) be the list of coins in each stack and let \(k\) be the number of coins you are allowed to pick. You need to find the maximum sum \(S\) defined as:

[ S = \sum_{i=1}^{k} c_i ]

where each \(c_i\) is one of the coin values from \(P\) and you are allowed to pick each coin at most once. It is guaranteed that \(k \le n\).

inputFormat

The input is given via standard input with the following format:

  • The first line contains an integer n representing the number of coin stacks.
  • The second line contains n space-separated non-negative integers representing the number of coins in each stack.
  • The third line contains an integer k representing the number of coins to pick.

outputFormat

Output via standard output a single integer, which is the maximum number of coins that can be collected after picking exactly k coins.

## sample
6
2 4 1 2 7 8
3
19