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 26, 2024 16:40E SequeiraE SequeiraScore: 84,700Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 26, 2024 16:4484,28677,477,24246,891,0001,995,0002,285,568
2Mar 26, 2024 16:4484,28677,847,62346,891,0001,995,0002,510,848
3Mar 26, 2024 16:4084,29176,921,85647,892,000997,0002,510,848
4Mar 26, 2024 16:4784,29780,159,34146,897,0001,995,0002,289,664
5Mar 26, 2024 16:4084,30776,450,81746,903,0001,995,0002,289,664
6Mar 26, 2024 16:4084,367106,779,22248,933,13602,207,744
7Mar 26, 2024 16:4084,37282,408,26246,939,0001,997,0002,285,568
8Mar 26, 2024 16:4784,39377,025,95746,951,0001,997,0002,502,656
9Mar 26, 2024 16:4084,40271,901,61346,955,0001,998,0002,289,664
10Mar 26, 2024 16:4484,40378,677,76246,956,0001,998,0002,510,848
11Mar 26, 2024 16:4784,42677,670,51947,968,000999,0002,510,848
12Mar 26, 2024 16:4784,45285,903,72046,983,0001,999,0002,285,568
13Mar 26, 2024 16:4784,46473,300,24447,990,000999,0002,510,848
14Mar 26, 2024 16:4484,50778,177,37447,014,0002,000,0002,289,664
15Mar 26, 2024 16:4784,55271,079,21047,039,0002,001,0002,510,848
16Mar 26, 2024 16:4084,58878,663,78047,059,0002,002,0002,506,752
17Mar 26, 2024 16:4484,60276,812,16048,068,0001,001,0002,289,664
18Mar 26, 2024 16:4084,608110,814,00749,072,61102,211,840
19Mar 26, 2024 16:4784,62471,983,27848,081,0001,001,0002,363,392
20Mar 26, 2024 16:4484,62475,680,43448,081,0001,001,0002,510,848
21Mar 26, 2024 16:4084,67672,493,28147,108,0002,004,0002,510,848
22Mar 26, 2024 16:4784,67872,965,09247,109,0002,004,0002,514,944
23Mar 26, 2024 16:4784,69172,688,29448,119,0001,002,0002,285,568
24Mar 26, 2024 16:4784,69574,758,87047,118,0002,005,0002,510,848
25Mar 26, 2024 16:4784,70071,616,75947,121,0002,005,0002,289,664
26Mar 26, 2024 16:4084,71674,912,62947,130,0002,005,0002,285,568
27Mar 26, 2024 16:4484,91469,804,78647,240,0002,010,0002,514,944
28Mar 26, 2024 16:4484,939128,660,73349,264,69502,224,128
29Mar 26, 2024 16:4784,980110,764,81349,288,40102,207,744
30Mar 26, 2024 16:4785,05774,829,17949,333,00002,289,664
31Mar 26, 2024 16:4085,06071,596,03449,335,00002,289,664
32Mar 26, 2024 16:4785,17878,866,46048,395,0001,008,0002,510,848
33Mar 26, 2024 16:4085,21271,418,14748,415,0001,008,0002,289,664
34Mar 26, 2024 16:4785,21972,537,95148,419,0001,008,0002,506,752
35Mar 26, 2024 16:4785,25579,467,84248,439,0001,009,0002,510,848
36Mar 26, 2024 16:4485,333110,629,11949,492,85902,211,840
37Mar 26, 2024 16:4485,333104,504,43349,492,93802,207,744
38Mar 26, 2024 16:4785,346137,602,03049,500,48902,211,840
39Mar 26, 2024 16:4485,40374,370,88949,534,00002,289,664
40Mar 26, 2024 16:4785,50377,265,19748,580,0001,012,0002,289,664
41Mar 26, 2024 16:4085,512101,596,83649,597,18202,224,128
42Mar 26, 2024 16:4785,52273,117,39549,603,00002,289,664
43Mar 26, 2024 16:4785,589117,080,26449,641,48402,215,936
44Mar 26, 2024 16:4785,595106,679,34749,645,02802,215,936
45Mar 26, 2024 16:4785,60282,472,81548,636,0001,013,0002,289,664
46Mar 26, 2024 16:4485,62274,000,87949,661,00002,289,664
47Mar 26, 2024 16:4785,635108,895,76549,668,27002,215,936
48Mar 26, 2024 16:4785,702108,365,86349,707,19502,215,936