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 17:52imotw2imotw2Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:13Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:17017,248,05307,218,00024,576No answer to stdout. Died or killed?stderr
2Dec 28, 2025 17:52144,238115,743,54279,627,0004,031,0002,412,544
3Dec 28, 2025 17:52145,167105,286,56680,188,0004,009,0002,342,912
4Dec 28, 2025 17:52145,329106,226,59879,274,0005,017,0002,342,912
5Dec 28, 2025 17:52145,505105,952,89979,370,0005,023,0002,506,752
6Dec 28, 2025 17:52145,795105,105,66877,515,0007,046,0002,367,488
7Dec 28, 2025 17:52145,836110,636,94379,551,0005,034,0002,367,488
8Dec 28, 2025 17:52146,066113,564,21979,676,0005,042,0002,383,872
9Dec 28, 2025 17:52146,443111,020,61680,940,0003,997,0002,502,656
10Dec 28, 2025 17:52146,602111,541,19180,028,0005,001,0002,379,776