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 listMay 5, 2026 14:58Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:30Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:42012,835,52102,135,00069,632Exit with code 2: unexpected fault address 0x4f63e0 fatal error: fault [signal SIGSEGV: segmentation violation code=0x2 addr=0x4f63e0 pc=0x51f8b9] goroutine 1 gp=0x1eb2f3a2e1e0 m=0 mp=0x5bef60 [running]: runtime.throw({0x48e3f4?, 0x477377?}) runtime/...stderr
2May 5, 2026 14:597,62421,921,6363,120,0001,302,0002,363,392
3May 5, 2026 14:598,20120,011,1903,318,0001,439,0002,379,776
4May 5, 2026 14:598,24618,021,9173,236,0001,547,0002,347,008
5May 5, 2026 14:598,37419,923,4983,238,0001,619,0002,379,776
6May 5, 2026 14:598,46322,681,2463,368,0001,541,0002,580,480
7May 5, 2026 14:598,49617,182,0673,242,0001,686,0002,543,616
8May 5, 2026 14:598,49823,254,9453,330,0001,599,0002,580,480
9May 5, 2026 14:598,93420,609,7423,536,0001,646,0002,510,848
10May 5, 2026 14:5943,40640,595,72824,124,0001,052,0002,433,024