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 24, 2026 17:07Jem BishopJem BishopError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
May 31, 2026 14:48Josu San Martin10.00RecheckSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1May 31, 2026 14:48010,292,1461,428,00002,121,728expected "1183139 ", got "1184147 "
2May 31, 2026 14:48010,317,3721,410,00002,125,824expected "979735 ", got "984448 "
3May 31, 2026 14:48010,240,9091,390,00002,125,824expected "759014 ", got "761499 "
4Feb 24, 2026 17:073,04517,915,73301,766,0002,392,064
5Feb 24, 2026 17:133,07219,144,39201,782,0002,342,912
6Feb 24, 2026 17:133,09128,446,4581,793,00002,433,024
7Feb 24, 2026 17:133,15217,513,30901,828,0002,404,352
8Feb 24, 2026 17:143,31019,325,357960,000960,0002,420,736
9Feb 24, 2026 17:133,35518,133,44701,946,0002,375,680
10Feb 24, 2026 17:133,35922,230,50601,948,0002,383,872
11Feb 24, 2026 17:143,36622,568,389976,000976,0002,441,216
12Feb 24, 2026 17:133,40218,479,12701,973,0002,371,584
13Feb 24, 2026 17:073,41719,153,28601,982,0002,416,640
14Feb 24, 2026 17:143,44119,777,688998,000998,0002,396,160
15Feb 24, 2026 17:073,57816,776,37302,075,0002,383,872