Sell Diminishing-Valued Colored Balls

MediumGreedy

Description

You have an inventory of colored balls to sell. The value of a ball is its inventory count. You want to sell exactly n balls. Return the maximum total value that you can attain after selling n balls.

Examples

Input:inventory = [2,5], orders = 4
Output:14
Explanation:

Greedy: sell highest value balls first. Ball worth 5, then 4, then 3, then 2. Total: 5+4+3+2=14.

Input:inventory = [3,5,1], orders = 6
Output:19
Explanation:

Selling the highest-valued balls greedily gives a total of 19, so the answer is 19.

Input:inventory = [1000000], orders = 3
Output:2999997
Explanation:

There are one type of ball with inventory count 1000000. Selling 3 balls with values 1000000, 999999, and 999998. Total: 1000000 + 999999 + 999998 = 2999997.

Constraints

  • 1 ≤ inventory.length ≤ 10^5
  • 1 ≤ orders ≤ min(sum(inventory), 10^9)

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!