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 listMar 21, 2026 00:14Dominique GarmierDominique GarmierError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 16:04Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 16:11024,373,9736,086,0004,882,00045,056Exit with code 2: panic: runtime error: index out of range [65536] with length 4096 goroutine 1 [running]: main.(*book).addOrder(0x3039f2af6000?, 0x0?, 0x80?) /src/main.go:97 +0x1d1 main.run({0x3039f1d80000, 0xd6c6e2, 0x55c320?}) /src/main.go:204 +0...stderr
2Mar 25, 2026 04:3618,59130,775,28610,783,00002,314,240
3Mar 25, 2026 04:3619,42231,157,28310,241,0001,024,0002,285,568
4Mar 21, 2026 00:1419,56026,619,53710,314,0001,031,0002,306,048
5Mar 21, 2026 00:1419,56432,769,4069,284,0002,063,0002,359,296
6Mar 25, 2026 04:3619,62636,557,42611,383,00002,306,048
7Mar 21, 2026 00:1420,30063,131,80410,793,000981,0002,306,048