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 listDec 28, 2025 22:51imotw2imotw2Score: 49,184Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 22:5247,73157,211,33724,608,0003,076,0002,404,352
2Dec 28, 2025 22:5148,20952,023,15423,967,0003,994,0002,404,352
3Dec 28, 2025 22:5248,24150,988,68823,983,0003,997,0002,416,640
4Dec 28, 2025 22:5648,55256,707,83222,126,0006,034,0002,428,928
5Dec 28, 2025 22:5648,79049,501,93624,256,0004,042,0002,322,432
6Dec 28, 2025 22:5148,88350,981,39423,289,0005,063,0002,383,872
7Dec 28, 2025 22:5149,10951,976,27323,397,0005,086,0002,490,368
8Dec 28, 2025 22:5649,18456,194,66923,433,0005,094,0002,449,408
9Dec 28, 2025 22:5149,84553,757,45823,926,0004,984,0002,375,680
10Dec 28, 2025 22:5149,85953,718,80624,787,0004,131,0002,363,392
11Dec 28, 2025 22:5250,33353,068,73325,167,0004,026,0002,338,816
12Dec 28, 2025 22:5150,59354,612,48524,285,0005,059,0002,408,448
13Dec 28, 2025 22:5151,05057,931,09023,483,0006,126,0002,293,760
14Dec 28, 2025 22:5151,56659,382,54625,921,0003,987,0002,506,752
15Dec 28, 2025 22:5151,58359,989,27523,935,0005,983,0002,289,664