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 listJun 12, 2026 23:11Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:26Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:35012,747,35502,088,000102,400Exit with code 2: unexpected fault address 0xff9f440 fatal error: fault [signal SIGSEGV: segmentation violation code=0x1 addr=0xff9f440 pc=0x10001372] goroutine 1 gp=0x225d0322a1e0 m=0 mp=0x525f60 [running]: runtime.throw({0x48e3f4?, 0x225d0324c530?})...stderr
2Jun 12, 2026 23:124,33211,948,4331,389,0001,124,0002,195,456
3Jun 12, 2026 23:124,53211,577,2601,408,0001,221,0002,195,456
4Jun 12, 2026 23:124,54411,683,9721,388,0001,248,0002,166,784
5Jun 12, 2026 23:124,61711,908,0171,445,0001,233,0002,195,456
6Jun 12, 2026 23:124,63212,826,4491,324,0001,363,0002,195,456
7Jun 12, 2026 23:124,65512,084,7181,451,0001,249,0002,166,784
8Jun 12, 2026 23:124,66311,866,1311,418,0001,287,0002,195,456
9Jun 12, 2026 23:1239,46030,659,40221,850,0001,037,0002,138,112
10Jun 12, 2026 23:1239,56232,557,28821,915,0001,031,0002,138,112