cswork / OA打开练习工作台

IBMOA #31

Get Maximum Amount

估计中等可在线判题

考点(AI 标注)单调栈与堆贪心调度

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.

输入格式

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 the maximum total revenue.

样例 1

输入:

5 6
10 10 8 9 1

输出:

55

样例 2

输入:

3 4
1 2 4

输出:

11

样例 3

输入:

3 7
5 5 5

输出:

30

样例说明

Each time, choose the category with the most remaining stock; the revenue increases by that stock count, and then the stock decreases by one.