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:35E SequeiraE SequeiraScore: 82,312Success
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:3578,86269,616,79744,724,0001,016,0002,273,280
2Mar 26, 2024 20:3579,31071,090,12545,000,0001,000,0002,363,392
3Mar 26, 2024 20:3579,35273,540,00644,023,0002,001,0002,273,280
4Mar 26, 2024 20:3579,49775,626,60844,104,0002,004,0002,506,752
5Mar 26, 2024 20:3579,51975,263,78045,119,0001,002,0002,506,752
6Mar 26, 2024 20:3579,64369,423,84344,185,0002,008,0002,273,280
7Mar 26, 2024 20:3581,79870,985,23646,434,0001,009,0002,519,040
8Mar 26, 2024 20:3581,96074,855,32346,526,0001,011,0002,285,568
9Mar 26, 2024 20:3581,97973,664,45246,537,0001,011,0002,285,568
10Mar 26, 2024 20:3582,10969,460,81346,610,0001,013,0002,510,848
11Mar 26, 2024 20:3582,14077,370,36847,641,00002,510,848
12Mar 26, 2024 20:3582,184106,135,13747,666,89202,228,224
13Mar 26, 2024 20:3582,233110,512,38647,695,09202,236,416
14Mar 26, 2024 20:3582,27975,409,87546,707,0001,015,0002,510,848
15Mar 26, 2024 20:3582,29776,494,89247,732,00002,285,568
16Mar 26, 2024 20:3582,31277,178,85646,726,0001,015,0002,285,568
17Mar 26, 2024 20:3582,320119,625,60947,745,83202,252,800
18Mar 26, 2024 20:3582,504114,536,69447,852,49502,220,032
19Mar 26, 2024 20:3582,550102,957,60747,878,83002,224,128
20Mar 26, 2024 20:3582,56475,324,25846,890,000997,0002,510,848
21Mar 26, 2024 20:3582,58184,370,44345,902,0001,995,0002,285,568
22Mar 26, 2024 20:3582,68170,462,34946,956,000999,0002,510,848
23Mar 26, 2024 20:3582,78475,277,09646,015,0002,000,0002,285,568
24Mar 26, 2024 20:3582,80974,382,96246,028,0002,001,0002,510,848
25Mar 26, 2024 20:3582,84372,212,37147,048,0001,001,0002,514,944
26Mar 26, 2024 20:3582,84874,312,51646,050,0002,002,0002,510,848
27Mar 26, 2024 20:3582,88679,175,01046,071,0002,003,0002,273,280
28Mar 26, 2024 20:3582,97970,597,58646,123,0002,005,0002,506,752
29Mar 26, 2024 20:3583,20370,734,41246,248,0002,010,0002,510,848
30Mar 26, 2024 20:3583,416117,383,47248,381,03602,215,936