#K12586. Maximize Magical Fruits
Maximize Magical Fruits
Maximize Magical Fruits
You are given n trees, each producing a certain number of magical fruits. Over k years, you can cut down exactly one tree per year. Once a tree is cut down, you get all its fruits. Your task is to determine the maximum total number of fruits you can collect by choosing k trees optimally.
Note: It is guaranteed that k ≤ n
.
The mathematical formulation of the problem is:
\[ \text{Maximize } S = \sum_{i=1}^{k} f_i, \quad \text{subject to choosing } k \text{ distinct trees from } n \]
where each f_i
is the number of fruits on the selected tree.
inputFormat
The input is read from stdin
and consists of two lines:
- The first line contains two integers
n
andk
separated by a space, wheren
is the number of trees andk
is the number of years. - The second line contains
n
space-separated integers representing the number of magical fruits produced by each tree.
outputFormat
Output a single integer to stdout
, which is the maximum number of magical fruits that can be collected by cutting down exactly k
trees.
5 3
10 20 30 40 50
120