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 listDec 28, 2025 18:11imotw2imotw2Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:39Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:45011,396,69001,319,00020,480No answer to stdout. Died or killed?
2Dec 28, 2025 18:1173,93470,549,16240,888,0001,994,0002,330,624
3Dec 28, 2025 18:1173,95764,251,52238,905,0003,990,0002,412,544
4Dec 28, 2025 18:1174,27665,650,63341,077,0002,003,0002,351,104
5Dec 28, 2025 18:1174,88168,034,31443,431,00002,281,472
6Dec 28, 2025 18:1175,67171,536,17242,892,000997,0002,326,528
7Dec 28, 2025 18:1175,68469,728,67241,902,0001,995,0002,363,392
8Dec 28, 2025 18:1176,71768,439,80840,451,0004,045,0002,478,080
9Dec 28, 2025 18:1177,36277,647,62143,873,000997,0002,396,160
10Dec 28, 2025 18:1178,71669,726,38743,626,0002,029,0002,285,568