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 19:18yenw0dyenw0dScore: 14,169Success
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 19:1813,72925,244,4852,986,0004,977,0002,355,200
2Dec 28, 2025 15:5613,81427,920,0452,003,0006,009,0002,318,336
3Dec 28, 2025 15:5613,84030,505,4043,010,0005,017,0002,314,240
4Dec 28, 2025 15:5613,90731,343,0034,033,0004,033,0002,310,144
5Dec 28, 2025 19:1814,06622,870,6414,079,0004,079,0002,314,240
6Dec 28, 2025 19:1814,14827,858,1843,077,0005,129,0002,387,968
7Dec 28, 2025 15:5614,15925,382,5322,053,0006,159,0002,383,872
8Dec 28, 2025 15:5614,16925,606,5314,109,0004,109,0002,469,888
9Dec 28, 2025 15:5614,18423,738,4953,085,0005,142,0002,318,336
10Dec 28, 2025 19:1814,20025,334,2412,059,0006,177,0002,297,856
11Dec 28, 2025 15:5614,22124,191,6294,124,0004,124,0002,326,528
12Dec 28, 2025 19:1814,22927,858,3982,063,0006,190,0002,514,944
13Dec 28, 2025 15:5614,28424,567,9003,107,0005,178,0002,326,528
14Dec 28, 2025 15:5614,51430,361,4942,104,0006,314,0002,445,312
15Dec 28, 2025 19:1814,94531,745,9652,167,0006,501,0002,387,968