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 20:03E SequeiraE SequeiraScore: 82,259Success
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 20:0381,60376,074,05946,323,0001,007,0002,506,752
2Mar 26, 2024 20:0381,75071,410,86147,415,00002,351,104
3Mar 26, 2024 20:0481,81272,287,37246,442,0001,009,0002,510,848
4Mar 26, 2024 20:0481,81673,184,83046,444,0001,009,0002,285,568
5Mar 26, 2024 20:0381,82873,677,76046,451,0001,009,0002,285,568
6Mar 26, 2024 20:0381,88173,246,59846,481,0001,010,0002,269,184
7Mar 26, 2024 20:0381,93175,016,46546,509,0001,011,0002,506,752
8Mar 26, 2024 20:0381,96969,829,40246,531,0001,011,0002,285,568
9Mar 26, 2024 20:0482,13177,321,21746,623,0001,013,0002,506,752
10Mar 26, 2024 20:0482,23482,538,81246,682,0001,014,0002,260,992
11Mar 26, 2024 20:0482,24580,301,81346,688,0001,014,0002,510,848
12Mar 26, 2024 20:0382,25271,369,35146,691,0001,015,0002,510,848
13Mar 26, 2024 20:0482,25980,365,24346,695,0001,015,0002,285,568
14Mar 26, 2024 20:0482,278116,905,36447,721,37102,220,032
15Mar 26, 2024 20:0382,284118,661,87147,724,92202,224,128
16Mar 26, 2024 20:0382,602109,951,03147,909,40502,224,128
17Mar 26, 2024 20:0382,73370,784,25345,986,0001,999,0002,285,568
18Mar 26, 2024 20:0482,76976,683,46446,006,0002,000,0002,285,568
19Mar 26, 2024 20:0482,77670,429,80846,010,0002,000,0002,285,568
20Mar 26, 2024 20:0382,84177,526,18746,046,0002,002,0002,285,568
21Mar 26, 2024 20:0482,99770,445,96846,133,0002,005,0002,510,848
22Mar 26, 2024 20:0483,078104,790,60448,185,04302,220,032
23Mar 26, 2024 20:0483,457106,342,34548,404,97902,232,320
24Mar 26, 2024 20:0383,501109,222,30948,430,29302,228,224