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 listApr 27, 2026 13:27Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 5, 2026 05:49Tomislav Tunkovic10.66CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 5, 2026 06:12010,749,132996,000020,480No answer to stdout. Died or killed?
2Apr 27, 2026 13:2747,31345,065,15821,344,0006,098,0002,347,008
3Apr 27, 2026 13:2748,42742,580,68324,076,0004,012,0002,301,952
4Apr 27, 2026 13:2749,02247,475,05923,356,0005,077,0002,326,528
5Apr 27, 2026 13:2749,45045,839,40124,725,0003,956,0002,334,720
6Apr 27, 2026 13:2750,12247,672,00324,059,0005,012,0002,293,760
7Apr 27, 2026 13:2750,47048,437,00926,245,0003,028,0002,318,336
8Apr 27, 2026 13:2750,48147,548,07326,251,0003,028,0002,330,624
9Apr 27, 2026 13:2750,50150,929,70126,261,0003,030,0002,301,952
10Apr 27, 2026 13:2750,90047,750,39825,450,0004,072,0002,543,616