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 17:37HattonuriHattonuriScore: 26,822Success
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 17:4825,93129,972,58814,038,0001,002,0002,301,952
2Dec 28, 2025 17:3626,03337,120,50314,093,0001,006,0002,469,888
3Dec 28, 2025 17:3626,33832,919,89915,276,00002,338,816
4Dec 28, 2025 17:4826,42432,982,89915,326,00002,297,856
5Dec 28, 2025 17:3826,52439,613,12515,384,00002,486,272
6Dec 28, 2025 17:3626,53629,773,50815,391,00002,523,136
7Dec 28, 2025 17:3826,65236,553,83714,428,0001,030,0002,465,792
8Dec 28, 2025 17:3626,68632,379,44415,478,00002,318,336
9Dec 28, 2025 17:4826,71641,677,68015,495,00002,494,464
10Dec 28, 2025 17:3626,72429,690,79114,467,0001,033,0002,478,080
11Dec 28, 2025 17:3826,77134,428,12414,492,0001,035,0002,535,424
12Dec 28, 2025 17:3726,77634,435,13014,495,0001,035,0002,531,328
13Dec 28, 2025 17:3826,77930,425,27714,497,0001,035,0002,326,528
14Dec 28, 2025 17:3826,82235,150,67014,520,0001,037,0002,441,216
15Dec 28, 2025 17:3826,84838,326,60014,534,0001,038,0002,424,832
16Dec 28, 2025 17:3626,85333,199,98415,575,00002,428,928
17Dec 28, 2025 17:3826,86036,472,65615,579,00002,494,464
18Dec 28, 2025 17:3726,86631,693,73615,582,00002,449,408
19Dec 28, 2025 17:3726,88636,693,07614,555,0001,039,0002,531,328
20Dec 28, 2025 17:3626,89532,354,76315,599,00002,527,232
21Dec 28, 2025 17:3726,89834,159,25615,601,00002,510,848
22Dec 28, 2025 17:3826,98436,647,66014,608,0001,043,0002,310,144
23Dec 28, 2025 17:3627,01744,988,24214,626,0001,044,0002,523,136
24Dec 28, 2025 17:3827,01740,641,60115,670,00002,428,928
25Dec 28, 2025 17:3727,15942,102,30114,702,0001,050,0002,478,080
26Dec 28, 2025 17:3627,24341,602,18914,748,0001,053,0002,445,312
27Dec 28, 2025 17:3727,42939,382,46713,921,0001,988,0002,510,848