AmazonOA #82
Maximize Rental Revenue (VM Stock)
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsSimulation
There are n kinds of virtual machines, with initial stock vmStock[i]. Serve up to m customers in order. Each customer first pays the sum of the current maximum stock and the current minimum non-zero stock, then rents one machine from a type that currently has the maximum stock, decreasing its stock by one. Zero stocks are excluded from the minimum. Based on the consistent behavior of the fixed source's Python, Java, and C++ code, this site specifies: stop immediately once all stock is exhausted and output the revenue accumulated so far; when the maximum is tied, choosing any of them does not affect the result.
Input
The first line contains two integers n m, and the second line contains n integers vmStock[i]. Complete original constraints: 1≤n≤10^6, 1≤m≤10^6, 1≤vmStock[i]≤10^6. The size limit for a single test input on this site is 32 MiB.
Output
Output the accumulated revenue, using an integer type large enough to hold 2×10^12. Customers who arrive after the stock runs out generate no revenue.
Example 1
Input:
2 3
2 3
Output:
12
Example 2
Input:
1 5
1
Output:
2
Example 3
Input:
1 3
3
Output:
12
Example notes
Example 1 charges 5, 4, 3 in turn, 12 in total; example 2 can only serve the first customer, revenue 2; example 3 charges 6, 4, 2 in turn, 12 in total.
Editorial
Loading solution…