IBMOA #31
Get Maximum Amount
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsGreedy
There are n categories of items, and category i currently has quantity[i] items left. Each time an item is sold, its price equals the number of items left in that category before the sale. Sell exactly m items in the optimal order and find the maximum total revenue. Valid input must have at least m items in stock; this is the feasibility condition for "selling exactly m items", which the source's numeric bounds do not state. Both source example outputs were confirmed under the rules by independent brute force.
Input
The first line contains n and m, and the second line contains n integers quantity[i]. Source ranges apply: 1≤n,m≤100000, 1≤quantity[i]≤100000; this site explicitly requires m≤sum(quantity) so that selling exactly m items is feasible.
Output
Output the maximum total revenue.
Example 1
Input:
5 6
10 10 8 9 1
Output:
55
Example 2
Input:
3 4
1 2 4
Output:
11
Example 3
Input:
3 7
5 5 5
Output:
30
Example notes
Each time, choose the category with the most remaining stock; the revenue increases by that stock count, and then the stock decreases by one.
Editorial
Loading solution…