Maximize Revenue from Falling Color Inventories. inventory[i] is the stock of color i. Selling one ball of that color earns its current stock count, then decreases that count by one.
### Problem
`inventory[i]` is the stock of color i. Selling one ball of that color earns its current stock count, then decreases that count by one. Sell exactly `orders` balls, choosing the colors and sale order freely. Return the largest total revenue modulo `10^9 + 7`.
```cpp
class Solution {
public:
int maxProfit(vector<int>& inventory, int orders);
};
```
### Examples
**Example 1:**
```text
inventory = [1,4], orders = 3
Output: 9
Explanation: Sell from the second color at values 4, 3, and 2.
```
**Example 2:**
```text
inventory = [3,3], orders = 4
Output: 10
Explanation: The four largest available values are 3, 3, 2, and 2.
```
### Constraints
- `1 <= inventory.length <= 10^5`.
- `1 <= inventory[i] <= 10^9`.
- `1 <= orders <= min(sum(inventory), 10^9)`.
### Clarifying Questions
The value is measured before the sale. Only the maximum revenue is returned; several color orders can attain that same value.
```hint Inspect the structure
When is a ball's value measured, and what does selling exactly `orders` require?
```
### Reasoning checkpoints
- Account for changing sale values and sell exactly the requested number of balls.
- Prove that the revenue is maximal and handle the stated stock and order bounds without arithmetic overflow.
### Optional extensions
How could sorting inventory heights replace the cutoff binary search?
Overview: Maximize Revenue from Falling Color Inventories. inventory[i] is the stock of color i. Selling one ball of that color earns its current stock count, then decreases that count by one.
inventory[i] is the stock of color i. Selling one ball of that color earns its current stock count, then decreases that count by one. Sell exactly orders balls, choosing the colors and sale order freely. Return the largest total revenue modulo 10^9 + 7.
class Solution {
public:
int maxProfit(vector<int>& inventory, int orders);
};
Examples
Example 1:
inventory = [1,4], orders = 3
Output: 9
Explanation: Sell from the second color at values 4, 3, and 2.
Example 2:
inventory = [3,3], orders = 4
Output: 10
Explanation: The four largest available values are 3, 3, 2, and 2.
Constraints
1 <= inventory.length <= 10^5
.
1 <= inventory[i] <= 10^9
.
1 <= orders <= min(sum(inventory), 10^9)
.
Clarifying Questions Guidance
The value is measured before the sale. Only the maximum revenue is returned; several color orders can attain that same value.
Reasoning checkpoints
Account for changing sale values and sell exactly the requested number of balls.
Prove that the revenue is maximal and handle the stated stock and order bounds without arithmetic overflow.
Optional extensions
How could sorting inventory heights replace the cutoff binary search?