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:04E SequeiraE SequeiraScore: 82,430Success
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:4481,36670,312,83945,184,0002,008,0002,510,848
2Mar 26, 2024 20:0481,40275,009,32045,204,0002,009,0002,285,568
3Mar 26, 2024 20:0481,40271,878,45945,204,0002,009,0002,519,040
4Mar 26, 2024 20:4481,65274,637,41446,351,0001,007,0002,285,568
5Mar 26, 2024 20:0481,791112,571,32147,439,01002,220,032
6Mar 26, 2024 20:4481,92271,846,86147,515,00002,506,752
7Mar 26, 2024 20:0481,93170,283,00546,509,0001,011,0002,285,568
8Mar 26, 2024 20:0481,940110,095,68347,525,25302,224,128
9Mar 26, 2024 20:0482,15273,125,81546,635,0001,013,0002,285,568
10Mar 26, 2024 20:0482,21473,475,90546,670,0001,014,0002,260,992
11Mar 26, 2024 20:4482,31281,602,70246,726,0001,015,0002,510,848
12Mar 26, 2024 20:0482,35074,042,86146,747,0001,016,0002,285,568
13Mar 26, 2024 20:4482,430114,671,29447,809,42002,224,128
14Mar 26, 2024 20:4482,56771,418,24945,894,0001,995,0002,359,296
15Mar 26, 2024 20:0482,60079,508,78746,910,000998,0002,285,568
16Mar 26, 2024 20:0482,68671,703,72446,959,000999,0002,510,848
17Mar 26, 2024 20:0482,70973,042,94345,973,0001,998,0002,285,568
18Mar 26, 2024 20:0482,746129,298,54047,992,84202,232,320
19Mar 26, 2024 20:4482,82272,669,19846,036,0002,001,0002,519,040
20Mar 26, 2024 20:4482,91472,078,86646,087,0002,003,0002,269,184
21Mar 26, 2024 20:4482,998106,332,82648,139,10102,228,224
22Mar 26, 2024 20:4483,02671,645,08647,152,0001,003,0002,510,848
23Mar 26, 2024 20:4483,164108,133,18948,235,01202,215,936
24Mar 26, 2024 20:4483,64073,357,24048,511,00002,351,104