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 31, 2025 20:04Yuriy LyfenkoYuriy LyfenkoScore: 57,260Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 31, 2025 20:0054,01257,810,04029,306,0002,021,0002,510,848
2Mar 31, 2025 20:0054,44354,459,24131,577,00002,453,504
3Mar 31, 2025 20:0955,00762,919,32629,910,0001,994,0002,506,752
4Mar 31, 2025 20:0055,52861,670,60730,194,0002,012,0002,498,560
5Mar 31, 2025 20:0055,70953,672,50429,282,0003,029,0002,265,088
6Mar 31, 2025 20:0055,71956,142,72832,317,00002,506,752
7Mar 31, 2025 20:0055,94857,973,34332,450,00002,519,040
8Mar 31, 2025 20:0055,97860,557,99231,453,0001,014,0002,273,280
9Mar 31, 2025 20:0056,03457,209,42131,485,0001,015,0002,506,752
10Mar 31, 2025 20:0056,46960,850,97631,729,0001,023,0002,404,352
11Mar 31, 2025 20:0056,62462,893,23932,842,00002,420,736
12Mar 31, 2025 20:0056,71957,980,28829,907,0002,990,0002,478,080
13Mar 31, 2025 20:0056,74358,383,73730,917,0001,994,0002,392,064
14Mar 31, 2025 20:0056,84562,113,46231,971,000999,0002,433,024
15Mar 31, 2025 20:0457,20557,233,21631,169,0002,010,0002,404,352
16Mar 31, 2025 20:0057,26057,127,52931,199,0002,012,0002,293,760
17Mar 31, 2025 20:0057,81259,549,18432,515,0001,016,0002,473,984
18Mar 31, 2025 20:0058,40361,676,80730,886,0002,988,0002,412,544
19Mar 31, 2025 20:0358,85756,384,01233,133,0001,004,0002,379,776
20Mar 31, 2025 20:0959,57856,705,14033,539,0001,016,0002,510,848
21Mar 31, 2025 20:0360,13869,284,24834,880,00002,478,080
22Mar 31, 2025 20:0060,16465,360,83232,901,0001,994,0002,400,256
23Mar 31, 2025 20:0360,41257,570,11133,037,0002,002,0002,265,088
24Mar 31, 2025 20:0461,09556,857,11934,423,0001,012,0002,453,504
25Mar 31, 2025 20:0062,46964,319,25834,220,0002,012,0002,482,176
26Mar 31, 2025 20:0063,84060,004,31736,027,0001,000,0002,351,104
27Mar 31, 2025 20:0065,70560,820,66936,104,0002,005,0002,347,008
28Mar 31, 2025 20:0467,26661,302,02037,014,0002,000,0002,457,600
29Mar 31, 2025 20:0068,05362,726,73638,459,0001,012,0002,473,984
30Mar 31, 2025 20:0973,28867,661,22741,495,0001,012,0002,424,832