#K75077. Maximize Units Sold
Maximize Units Sold
Maximize Units Sold
You are given a simulation problem for a food warehouse. The warehouse manages a list of events over n days, where each event is either a shipment receiving operation or an order processing operation.
At the beginning of each day, you may receive a shipment of a food item in the format receive <item> <quantity>
. The warehouse can stock at most k distinct items at any time. If an item is already in stock or there is available capacity (i.e. the number of distinct items in stock is less than k) then the shipment will be accepted and its quantity added to the existing stock.
For an event in the format order <item> <quantity>
, if the item exists in the warehouse and has a positive stock, you fulfill the order by selling as many units as possible (up to the order quantity) from the available stock. After processing an order, if the stock of that item drops to zero, it is removed from the warehouse.
Your task is to simulate these events and compute the total number of units sold. Formally, you are given two integers \(n\) and \(k\), followed by \(n\) events. You must output the maximum total units sold.
Note: The capacity constraint is that at most \(k\) distinct items can be stored at once.
inputFormat
The first line contains two integers (n) and (k) separated by a space, where (n) is the number of days/events and (k) is the warehouse capacity (maximum distinct items). The next (n) lines each contain an event in one of the following formats: receive <item> <quantity>
or order <item> <quantity>
.
outputFormat
Output a single integer representing the total number of units sold after processing all events.## sample
5 2
receive apple 100
receive banana 150
order apple 50
receive orange 200
order banana 100
150
</p>