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 listAug 18, 2025 20:46CadovvlCadovvlScore: 70,236Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 13, 2025 13:2169,59067,232,86339,353,0001,009,0002,392,064
2Aug 18, 2025 20:4269,60366,750,79640,370,00002,379,776
3Aug 11, 2025 20:4969,61666,138,46840,377,00002,379,776
4Aug 11, 2025 20:4969,77961,783,91239,461,0001,011,0002,519,040
5Aug 18, 2025 20:4669,85965,551,18340,518,00002,519,040
6Aug 11, 2025 20:4769,90266,487,05640,543,00002,519,040
7Aug 13, 2025 13:2169,90766,306,28539,533,0001,013,0002,519,040
8Aug 18, 2025 20:4269,92864,675,57939,545,0001,013,0002,260,992
9Aug 11, 2025 20:4769,98665,597,49239,578,0001,014,0002,506,752
10Aug 13, 2025 13:2170,00065,830,00739,585,0001,015,0002,383,872
11Aug 11, 2025 20:4970,01664,255,57439,594,0001,015,0002,387,968
12Aug 18, 2025 20:4270,01763,367,13939,595,0001,015,0002,519,040
13Aug 13, 2025 13:2170,02865,205,74439,601,0001,015,0002,392,064
14Aug 18, 2025 20:4270,02966,821,46239,602,0001,015,0002,519,040
15Aug 13, 2025 13:2170,04763,022,45640,627,00002,387,968
16Aug 11, 2025 20:4970,04860,897,00140,628,00002,260,992
17Aug 11, 2025 20:4870,06268,015,42440,636,00002,392,064
18Aug 13, 2025 13:2170,06261,489,13340,636,00002,383,872
19Aug 18, 2025 20:4270,07271,711,69939,626,0001,016,0002,506,752
20Aug 11, 2025 20:4770,08670,899,53640,650,00002,519,040
21Aug 11, 2025 20:4970,21766,345,57738,740,0001,986,0002,519,040
22Aug 13, 2025 13:2170,23666,743,29639,719,0001,018,0002,506,752
23Aug 11, 2025 20:4770,25965,856,39939,732,0001,018,0002,260,992
24Aug 11, 2025 20:4770,27165,791,91239,739,0001,018,0002,260,992
25Aug 18, 2025 20:4270,27665,720,94539,741,0001,019,0002,260,992
26Aug 11, 2025 20:4970,28164,169,51139,744,0001,019,0002,260,992
27Aug 18, 2025 20:4270,28366,948,58039,745,0001,019,0002,519,040
28Aug 11, 2025 20:4870,48365,414,13438,886,0001,994,0002,519,040
29Aug 11, 2025 20:4870,48366,970,83138,886,0001,994,0002,260,992
30Aug 18, 2025 20:4270,48669,713,85340,882,00002,383,872
31Aug 13, 2025 13:2170,49767,614,55039,891,000997,0002,379,776
32Aug 11, 2025 20:4770,50064,766,63539,868,0001,022,0002,260,992
33Aug 18, 2025 20:4270,50267,192,14939,894,000997,0002,506,752
34Aug 11, 2025 20:4970,52269,086,40038,908,0001,995,0002,392,064
35Aug 13, 2025 13:2170,52469,606,51839,907,000997,0002,260,992
36Aug 18, 2025 20:4670,52966,326,04238,912,0001,995,0002,519,040
37Aug 18, 2025 20:4670,57666,458,37038,938,0001,996,0002,260,992
38Aug 11, 2025 20:4770,58367,944,29639,940,000998,0002,260,992
39Aug 11, 2025 20:4970,70365,029,20039,008,0002,000,0002,256,896
40Aug 11, 2025 20:4770,71271,987,16139,013,0002,000,0002,519,040
41Aug 11, 2025 20:4770,99070,029,45839,166,0002,008,0002,506,752
42Aug 11, 2025 20:4971,06470,021,32040,212,0001,005,0002,260,992