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:13E SequeiraE SequeiraScore: 82,319Success
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:1381,23369,507,38245,111,0002,004,0002,510,848
2Mar 26, 2024 20:1381,45769,012,78347,245,00002,285,568
3Mar 26, 2024 20:1381,57874,034,68847,315,00002,506,752
4Mar 26, 2024 20:1381,66970,003,28046,361,0001,007,0002,285,568
5Mar 26, 2024 20:1381,72274,376,71247,399,00002,285,568
6Mar 26, 2024 20:1381,91770,972,58046,502,0001,010,0002,285,568
7Mar 26, 2024 20:1381,94170,524,06946,515,0001,011,0002,285,568
8Mar 26, 2024 20:1382,02873,695,40146,564,0001,012,0002,285,568
9Mar 26, 2024 20:1382,17274,358,79546,646,0001,014,0002,285,568
10Mar 26, 2024 20:1382,238112,711,69047,698,31302,220,032
11Mar 26, 2024 20:1382,25276,888,68846,691,0001,015,0002,519,040
12Mar 26, 2024 20:1382,27874,423,75546,706,0001,015,0002,285,568
13Mar 26, 2024 20:1382,319129,105,23247,744,81102,224,128
14Mar 26, 2024 20:1382,54375,796,54845,881,0001,994,0002,285,568
15Mar 26, 2024 20:1382,55775,542,36245,888,0001,995,0002,510,848
16Mar 26, 2024 20:1382,57474,766,46146,896,000997,0002,510,848
17Mar 26, 2024 20:1382,58479,136,36945,904,0001,995,0002,342,912
18Mar 26, 2024 20:1382,74071,330,03246,990,000999,0002,285,568
19Mar 26, 2024 20:1382,756107,599,55847,998,19802,228,224
20Mar 26, 2024 20:1382,77269,953,71546,008,0002,000,0002,285,568
21Mar 26, 2024 20:1382,774110,356,68448,009,04102,220,032
22Mar 26, 2024 20:1382,90577,789,64747,084,0001,001,0002,285,568
23Mar 26, 2024 20:1382,912111,729,77848,089,01402,220,032
24Mar 26, 2024 20:1383,656119,625,13548,520,54102,220,032