Order book Yuriy Lyfenko

Simulate a sell-side order book and calculate the cost of a final purchase as fast as possible.

Input

1,000,000 order updates on STDIN, one per line:

  • + <price> <size> – add a new sell order
  • - <position> – delete the order at the given position
  • = <size> – buy <size> shares from the top of the order book

After all updates are processed, buy 1,000 shares from the top of the order book.

Output

Print the total cost of the final 1,000-share purchase to STDOUT.

Order Book Rules

Orders are sorted by price ascending (lower is better). Orders at the same price are sorted by arrival time (earlier first). Position 0 is the best (lowest-price) offer.

The = (buy) operation consumes shares starting from position 0. If an order is fully consumed, it is removed from the book.

Example

Input Order book state
+ 1137 100 (1137,100)
+ 1130 10 (1130,10), (1137,100)
+ 1130 50 (1130,10), (1130,50), (1137,100)
- 0 (1130,50), (1137,100)
+ 1150 200 (1130,50), (1137,100), (1150,200)
= 200 (1150,150)

Total cost of the last buy: 50 * 1130 + 100 * 1137 + 50 * 1150.

Back to listFeb 28, 2026 00:430xZohan0xZohanError
Source Code

Source code access is restricted. Log in to request access.

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:18Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:33010,962,122877,000024,576No answer to stdout. Died or killed?
2Feb 28, 2026 00:4369,27668,473,94839,176,0001,004,0002,289,664
3Feb 28, 2026 00:4369,37662,288,90639,233,0001,005,0002,351,104
4Feb 28, 2026 00:4369,45763,895,59138,271,0002,014,0002,351,104
5Feb 28, 2026 00:4369,67861,517,39539,403,0001,010,0002,281,472
6Feb 28, 2026 00:4369,70961,410,53240,431,00002,355,200
7Feb 28, 2026 00:4369,84865,464,45940,512,00002,449,408
8Feb 28, 2026 00:4369,90762,917,60439,533,0001,013,0002,289,664
9Feb 28, 2026 00:4370,17468,824,68939,684,0001,017,0002,281,472
10Feb 28, 2026 00:4370,23169,635,11440,734,00002,281,472