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 listMay 5, 2026 15:52Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 21:25Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 21:56010,878,452946,000020,480No answer to stdout. Died or killed?
2May 5, 2026 15:523,83618,784,1452,225,00002,514,944
3May 5, 2026 15:523,91718,372,4152,272,00002,478,080
4May 5, 2026 15:523,96221,129,7682,298,00002,490,368
5May 5, 2026 15:5236,79139,474,38221,339,00002,359,296
6May 5, 2026 15:5236,79439,914,16120,279,0001,062,0002,555,904
7May 5, 2026 15:5237,18638,578,92320,529,0001,039,0002,355,200
8May 5, 2026 15:5237,25344,935,45920,506,0001,101,0002,322,432
9May 5, 2026 15:5237,40838,243,44021,697,00002,355,200
10May 5, 2026 15:5238,56239,954,33221,256,0001,110,0002,424,832