Requirements
- Input: quantities for n product types and m customers. Each customer buys one item.
- A product’s price equals its current remaining quantity; selling one decreases that quantity by one.
- Return the maximum total revenue.
Notes
Always sell from a largest positive quantity. Each product contributes a decreasing sequence of possible sale prices, so selecting the largest available next price is optimal. A max-heap gives O(n + m log(n + 1)) time and O(n) space for n product types and m sales, provided enough stock exists. Confirm how insufficient stock should be handled.
For many sales, sort quantities and lower the highest group together. Lowering k products from height h to l consumes k(h-l) sales and earns k(h+l+1)(h-l)/2. For a partial level, use quotient and remainder to distribute the remaining sales. This takes O(n log n) time overall. Use sufficiently wide arithmetic, and do not introduce a modulus unless the actual task requires one.
Preparation
- In 20 minutes, implement a heap-based seller and compare its revenue with exhaustive choices on small inventories.
- In 20 minutes, replace repeated sales with descending price levels; test zero orders, tied quantities, and selling all available units.

