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 21, 2025 23:53HattonuriHattonuriScore: 31,226Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 23:5329,95240,349,22817,372,00002,510,848
2Dec 21, 2025 23:5430,09046,281,75016,426,0001,026,0002,523,136
3Dec 21, 2025 23:5330,17836,138,89016,474,0001,029,0002,310,144
4Dec 21, 2025 23:5230,52135,557,26017,702,00002,519,040
5Dec 21, 2025 23:5330,56232,628,19217,726,00002,334,720
6Dec 21, 2025 23:5030,61940,010,44617,759,00002,330,624
7Dec 21, 2025 23:5230,85733,184,36816,903,000994,0002,510,848
8Dec 21, 2025 23:5430,88142,125,29016,916,000995,0002,330,624
9Dec 21, 2025 23:5430,89844,186,80116,926,000995,0002,301,952
10Dec 21, 2025 23:5330,94142,473,69315,952,0001,994,0002,490,368
11Dec 21, 2025 23:5130,95743,893,92615,960,0001,995,0002,334,720
12Dec 21, 2025 23:5031,00733,659,96516,985,000999,0002,310,144
13Dec 21, 2025 23:5331,02845,606,12216,997,000999,0002,502,656
14Dec 21, 2025 23:5231,03841,707,48317,002,0001,000,0002,326,528
15Dec 21, 2025 23:5031,07438,741,38517,022,0001,001,0002,461,696
16Dec 21, 2025 23:5431,07437,617,10216,021,0002,002,0002,482,176
17Dec 21, 2025 23:5331,08342,447,51117,027,0001,001,0002,486,272
18Dec 21, 2025 23:5031,09337,077,72816,031,0002,003,0002,457,600
19Dec 21, 2025 23:5331,09839,664,14317,035,0001,002,0002,310,144
20Dec 21, 2025 23:5431,22642,568,91517,105,0001,006,0002,535,424
21Dec 21, 2025 23:5231,28637,815,50416,130,0002,016,0002,441,216
22Dec 21, 2025 23:5031,30243,238,20417,147,0001,008,0002,482,176
23Dec 21, 2025 23:5431,34840,158,11617,172,0001,010,0002,465,792
24Dec 21, 2025 23:5231,39750,410,58817,199,0001,011,0002,486,272
25Dec 21, 2025 23:5331,56745,979,99618,309,00002,453,504
26Dec 21, 2025 23:5231,63442,001,48518,348,00002,306,048
27Dec 21, 2025 23:5131,67233,593,66718,370,00002,478,080
28Dec 21, 2025 23:5431,72837,048,77418,402,00002,310,144
29Dec 21, 2025 23:5431,73144,464,71617,382,0001,022,0002,293,760
30Dec 21, 2025 23:5331,86238,481,23418,480,00002,527,232
31Dec 21, 2025 23:5431,86739,330,81417,457,0001,026,0002,445,312
32Dec 21, 2025 23:5431,90736,432,93017,478,0001,028,0002,453,504
33Dec 21, 2025 23:5032,13440,277,85718,638,00002,437,120
34Dec 21, 2025 23:5132,19335,996,25917,635,0001,037,0002,461,696
35Dec 21, 2025 23:5332,61944,894,06617,924,000995,0002,416,640
36Dec 21, 2025 23:5432,68839,636,42916,964,0001,995,0002,506,752
37Dec 21, 2025 23:5432,76044,770,33017,001,0002,000,0002,502,656
38Dec 21, 2025 23:5332,84536,216,98317,045,0002,005,0002,473,984
39Dec 21, 2025 23:5332,96442,835,21518,113,0001,006,0002,473,984