#K12586. Maximize Magical Fruits

    ID: 23723 Type: Default 1000ms 256MiB

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 and k separated by a space, where n is the number of trees and k 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.

## sample
5 3
10 20 30 40 50
120