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:53HattonuriHattonuriError
Source Code

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

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