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 18, 2025 16:41Wookyung JeongWookyung JeongScore: 37,405Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 18, 2025 16:4036,35950,971,84219,080,0002,008,0002,297,856
2Dec 18, 2025 16:4136,77941,598,01721,332,00002,502,656
3Dec 18, 2025 16:4037,11745,736,13421,528,00002,519,040
4Dec 18, 2025 16:4137,19344,634,42420,545,0001,027,0002,281,472
5Dec 18, 2025 16:4137,25343,388,48120,579,0001,028,0002,416,640
6Dec 18, 2025 16:4237,26642,762,47320,585,0001,029,0002,510,848
7Dec 18, 2025 16:4137,30050,427,98320,604,0001,030,0002,482,176
8Dec 18, 2025 16:4137,40541,459,43621,695,00002,256,896
9Dec 18, 2025 16:4237,76444,821,83319,912,0001,991,0002,506,752
10Dec 18, 2025 16:4137,78150,835,73220,917,000996,0002,482,176
11Dec 18, 2025 16:4137,79146,975,50219,927,0001,992,0002,461,696
12Dec 18, 2025 16:4137,83352,253,45719,949,0001,994,0002,428,928
13Dec 18, 2025 16:4137,88453,216,92019,976,0001,997,0002,449,408
14Dec 18, 2025 16:4037,91943,181,41620,994,000999,0002,506,752
15Dec 18, 2025 16:4238,06448,702,75821,074,0001,003,0002,482,176