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:46E SequeiraE SequeiraScore: 80,336Success
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:4775,97873,285,47443,066,0001,001,0002,506,752
2Mar 26, 2024 20:4776,51270,652,84043,369,0001,008,0002,510,848
3Mar 26, 2024 20:4676,56975,323,17343,401,0001,009,0002,285,568
4Mar 26, 2024 20:4676,81266,733,09443,539,0001,012,0002,277,376
5Mar 26, 2024 20:4676,84170,644,50243,556,0001,012,0002,289,664
6Mar 26, 2024 20:4776,87466,499,88243,574,0001,013,0002,289,664
7Mar 26, 2024 20:4779,75571,436,98744,247,0002,011,0002,285,568
8Mar 26, 2024 20:4679,79768,475,09544,270,0002,012,0002,285,568
9Mar 26, 2024 20:4679,82169,586,88944,284,0002,012,0002,285,568
10Mar 26, 2024 20:4779,92673,909,32446,357,00002,285,568
11Mar 26, 2024 20:4679,99766,506,53645,390,0001,008,0002,285,568
12Mar 26, 2024 20:4680,00273,162,40745,393,0001,008,0002,285,568
13Mar 26, 2024 20:4680,07273,567,90245,433,0001,009,0002,285,568
14Mar 26, 2024 20:4680,08672,904,45745,441,0001,009,0002,285,568
15Mar 26, 2024 20:4780,17676,407,97746,502,00002,510,848
16Mar 26, 2024 20:4780,33668,987,16645,583,0001,012,0002,285,568
17Mar 26, 2024 20:4680,40281,658,99445,620,0001,013,0002,351,104
18Mar 26, 2024 20:4780,54576,032,17945,701,0001,015,0002,285,568
19Mar 26, 2024 20:4780,82479,598,16344,884,0001,994,0002,285,568
20Mar 26, 2024 20:4780,85970,384,71344,903,0001,995,0002,285,568
21Mar 26, 2024 20:4681,02970,206,77344,998,0001,999,0002,506,752
22Mar 26, 2024 20:4781,12875,825,28745,052,0002,002,0002,510,848
23Mar 26, 2024 20:4681,14769,322,67245,063,0002,002,0002,510,848
24Mar 26, 2024 20:4781,16773,806,49446,076,0001,001,0002,285,568
25Mar 26, 2024 20:4781,792113,774,91347,439,54902,224,128
26Mar 26, 2024 20:4781,855118,323,82247,475,67102,224,128
27Mar 26, 2024 20:4682,028116,730,97947,576,27302,232,320
28Mar 26, 2024 20:4782,029106,786,76647,576,57102,228,224
29Mar 26, 2024 20:4682,402136,900,40647,792,91602,232,320
30Mar 26, 2024 20:4682,664111,969,98347,945,28102,228,224