FIFO inventory: cost of goods sold
A phone accessories shop in Dubai buys cables in batches at different prices and sells them over time. Accounting uses FIFO costing: when units are sold, they come from the oldest batch first, and the cost of goods sold (COGS) is the purchase cost of exactly those units, possibly spread across several batches.
Task: the first line is n, the number of events. Each of the next n lines is either BUY qty unit_cost or SELL qty unit_price (qty is an integer, prices have up to 2 decimals). For every SELL print, in order, SELL k: cogs=X profit=Y where k counts the SELL events starting at 1, X is the FIFO cost of the units sold, and Y is the sale revenue (qty times unit price) minus X, both with 2 decimals (profit may be negative, print the minus sign). If a sale asks for more units than are in stock, print SELL k: REJECTED and change nothing. After all events print Stock: Q units, value V with the remaining units and their FIFO cost (2 decimals).
Sample tests
Input
6 BUY 100 2.50 BUY 50 3.10 SELL 120 5.00 BUY 30 2.80 SELL 40 4.75 SELL 50 4.00
Expected output
SELL 1: cogs=312.00 profit=288.00 SELL 2: cogs=121.00 profit=69.00 SELL 3: REJECTED Stock: 20 units, value 56.00
Input
4 BUY 10 12.00 SELL 4 10.00 SELL 6 15.50 SELL 1 20.00
Expected output
SELL 1: cogs=48.00 profit=-8.00 SELL 2: cogs=72.00 profit=21.00 SELL 3: REJECTED Stock: 0 units, value 0.00
Your answer is also checked against 3 hidden tests.
Need a hint?
A deque of [quantity left, unit cost] lots works well: take from the left, and remove a lot when it is empty.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
