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 27, 2026 23:44Samuel VanderwaalSamuel VanderwaalError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:14Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:25017,630,25507,333,00024,576No answer to stdout. Died or killed?stderr
2Feb 27, 2026 23:4453,52156,322,36126,036,0005,006,0002,473,984
3Feb 27, 2026 23:4454,23161,635,32326,381,0005,073,0002,449,408
4Feb 27, 2026 23:4454,39151,122,35426,459,0005,088,0002,359,296
5Feb 27, 2026 23:4454,95358,062,80825,897,0005,976,0002,514,944
6Feb 27, 2026 23:4455,75060,408,93127,283,0005,052,0002,445,312
7Feb 27, 2026 23:4455,89056,866,37127,351,0005,065,0002,314,240
8Feb 27, 2026 23:4456,70257,896,46226,908,0005,979,0002,396,160
9Feb 27, 2026 23:4456,71959,788,57426,916,0005,981,0002,400,256
10Feb 27, 2026 23:4457,02859,135,39928,065,0005,011,0002,383,872