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 listMar 2, 2026 21:56JuneLJuneLScore: 57,447Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 2, 2026 21:5751,96951,716,59928,133,0002,009,0002,269,184
2Mar 2, 2026 21:5652,64552,581,96530,534,00002,494,464
3Mar 2, 2026 21:5653,29361,712,78327,919,0002,991,0002,502,656
4Mar 2, 2026 21:5653,40557,550,30628,977,0001,998,0002,510,848
5Mar 2, 2026 21:5753,44359,688,73028,998,0001,999,0002,494,464
6Mar 2, 2026 21:5753,97252,537,69530,295,0001,009,0002,416,640
7Mar 2, 2026 21:5655,67854,104,38730,275,0002,018,0002,478,080
8Mar 2, 2026 21:5656,03158,658,71532,498,00002,457,600
9Mar 2, 2026 21:5656,36761,624,87831,672,0001,021,0002,494,464
10Mar 2, 2026 21:5657,33358,310,47932,246,0001,007,0002,514,944
11Mar 2, 2026 21:5757,44755,788,91031,300,0002,019,0002,494,464
12Mar 2, 2026 21:5757,87655,999,68432,551,0001,017,0002,404,352
13Mar 2, 2026 21:5658,48157,256,83131,924,0001,995,0002,490,368
14Mar 2, 2026 21:5758,68656,554,80732,036,0002,002,0002,416,640
15Mar 2, 2026 21:5761,95760,317,86033,939,0001,996,0002,387,968
16Mar 2, 2026 21:5662,47463,190,32634,222,0002,013,0002,433,024
17Mar 2, 2026 21:5663,68170,962,54734,939,0001,996,0002,408,448
18Mar 2, 2026 21:5664,48659,461,92136,392,0001,010,0002,478,080
19Mar 2, 2026 21:5766,77468,395,19537,710,0001,019,0002,412,544
20Mar 2, 2026 21:5671,84773,424,83740,655,0001,016,0002,387,968
21Mar 2, 2026 21:5773,54168,714,69541,639,0001,015,0002,490,368