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 listFeb 11, 2026 15:20Code HintsCode HintsError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 15:00Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 15:54010,253,193730,000020,480No answer to stdout. Died or killed?
2Feb 11, 2026 15:2039,85544,394,51421,106,0002,010,0002,514,944
3Feb 11, 2026 15:2040,17144,403,71023,299,00002,494,464
4Feb 11, 2026 15:2041,27647,689,23722,943,000997,0002,482,176
5Feb 11, 2026 15:2041,45749,894,07622,042,0002,003,0002,490,368
6Feb 11, 2026 15:2042,37250,779,94123,552,0001,024,0002,486,272
7Feb 11, 2026 15:2042,57956,224,57223,667,0001,029,0002,502,656
8Feb 11, 2026 15:2043,22648,827,76024,069,0001,002,0002,441,216
9Feb 11, 2026 15:2043,42245,376,14324,178,0001,007,0002,478,080
10Feb 11, 2026 15:2044,23453,634,92224,630,0001,026,0002,441,216