#C11283. Maximum Project Profit

    ID: 40582 Type: Default 1000ms 256MiB

Maximum Project Profit

Maximum Project Profit

You are given N projects with corresponding profit values, represented as an array \(P = [P_1, P_2, \dots, P_N]\). You are allowed to select at most K projects. However, note that only projects with a positive profit add value. Your goal is to compute the maximum total profit achievable by selecting up to \(K\) projects.

To solve this problem, sort the array \(P\) in descending order. Then, select the first \(K\) elements and sum only the positive values among them.

The mathematical formulation is:

$$ \text{MaxProfit} = \sum_{i=1}^{K} \max(P'_i, 0) $$

where \(P'_i\) is the \(i\)-th element in the sorted (in descending order) list \(P\).

inputFormat

The input is given via standard input (stdin) with the following format:

  • The first line contains two space-separated integers, \(N\) (the number of projects) and \(K\) (the maximum number of projects you can select).
  • The second line contains \(N\) space-separated integers representing the profit values \(P_1, P_2, \dots, P_N\) of the projects.

outputFormat

Output a single integer to the standard output (stdout): the maximum profit achievable by selecting up to \(K\) projects.

## sample
5 3
10 -5 15 20 -10
45