AmazonOA #356
VM Rental Revenue
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsGreedy
Each time, first charge the sum of the current maximum stock and the current minimum non-zero stock, then rent out one unit from any type with the maximum stock. Zero stock is not considered for the minimum, and ties for the maximum can be broken arbitrarily. Compute the total income over all requests.
Input
The first line contains n m and the second line contains n stock values. OCR313 gives n≤10^6, while 314 gives 10^5 with stock≤10^6. This site covers the broader sourced domain: 1≤n≤10^6, 0≤stock[i]≤10^6, 0≤m≤sum(stock)≤10^12, without imposing sum>m.
Output
Output a single integer, the answer.
Example 1
Input:
3 4
1 2 4
Output:
15
Example 2
Input:
4 4
2 1 1 3
Output:
12
Example 3
Input:
3 3
10 10 11
Output:
60
Example notes
The examples give 15, 12 and 60. The 1+4=6 in the OCR313 table is an arithmetic typo and should be 5; the 5 VM types in 316 do not match the three-item input and should be 3 types.
Editorial
Loading solution…