KatInterview

Top-K Price Harvest from Tiered Stock Levels

Heaps, Sorting & SearchAuto-graded · 24 test cases
MediumAuto-gradedHeaps, Sorting & Searchgreedysortingmath

Last updated

Tests whether a candidate can reformulate a greedy revenue problem into summing the largest elements across union-of-range price sets and implement an O(n log n) layer-sweep solution.

Problem

A warehouse manages n product lines. Each product line i (0-indexed) holds stock[i] units. A queue of m buyers arrives one at a time. Each buyer purchases exactly one unit of one product line (the warehouse decides which), as long as that product line still has units available.

How the sale price is determined:

When a buyer purchases a unit from product line i, they pay a price equal to the current unit count of that product line at the moment of the transaction — that is, before the unit is removed. The unit count then drops by 1.

For example, if product line i currently holds 4 units and a buyer purchases from it, that buyer pays 4 and the count becomes 3 for the next transaction.

The warehouse can freely decide, for each buyer in turn, which product line to sell from (as long as it has positive stock). Your task is to determine the maximum total revenue the warehouse can collect across all buyers.

If m exceeds the total units available across all product lines, the surplus buyers simply receive nothing; revenue comes only from actual sales.

You must design a solution that remains efficient even when n, m, and individual stock values are very large (up to 10⁹).


Input

  • n — a positive integer, the number of product lines.
  • stock — a list of n non-negative integers, where stock[i] is the initial unit count of product line i.
  • m — a non-negative integer, the number of buyers.

Output

Return a single integer: the maximum total revenue achievable.


Constraints

  • 1 ≤ n ≤ 10⁵
  • 0 ≤ stock[i] ≤ 10⁹
  • 0 ≤ m ≤ 10⁹
  • The answer fits in a 64-bit signed integer (Python handles this natively).

Examples

Example 1

Input: max_warehouse_revenue(2, [2, 3], 3)

Output: 7

Example 2

Input: max_warehouse_revenue(3, [5, 1, 4], 4)

Output: 16

Example 3

Input: max_warehouse_revenue(4, [10, 10, 10, 10], 7)

Output: 67

Example 4

Input: max_warehouse_revenue(3, [1, 2, 3], 10)

Output: 10

Hints

Hint 1

Think about what set of prices is theoretically available for sale across all product lines. A product line with initial stock q can generate prices q, q-1, q-2, ..., 1 — one per unit. To maximize revenue, which prices from that full collection should you pick?

Hint 2

Once you see the answer as 'sum the m largest values from the union of all those price ranges', think about how to compute that sum without enumerating every individual price. Prices form horizontal bands when you sort the stock values and look at them as a histogram.

Hint 3

Sort stocks in descending order and sweep downward band by band. At each band, you know exactly how many product lines are 'active' (their stock reaches this level). Use closed-form arithmetic-series formulas to consume entire bands in O(1), stopping as soon as you've accounted for all m buyers.