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 27, 2024 20:48E SequeiraE SequeiraScore: 82,393Success
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 20:4880,87971,157,37644,914,0001,996,0002,506,752
2Mar 27, 2024 20:4880,91675,304,81444,934,0001,997,0002,285,568
3Mar 27, 2024 20:4881,16773,824,40446,076,0001,001,0002,285,568
4Mar 27, 2024 20:4881,21674,778,30245,101,0002,004,0002,285,568
5Mar 27, 2024 20:4881,21773,418,54645,102,0002,004,0002,265,088
6Mar 27, 2024 20:4881,22274,956,24646,107,0001,002,0002,273,280
7Mar 27, 2024 20:4881,48370,704,95945,249,0002,011,0002,281,472
8Mar 27, 2024 20:4881,52467,432,40845,272,0002,012,0002,281,472
9Mar 27, 2024 20:4881,67268,511,40346,363,0001,007,0002,285,568
10Mar 27, 2024 20:4881,73870,682,86946,400,0001,008,0002,285,568
11Mar 27, 2024 20:4881,86969,918,91447,484,00002,510,848
12Mar 27, 2024 20:4882,08871,972,56046,598,0001,013,0002,285,568
13Mar 27, 2024 20:4882,39373,120,60947,788,00002,506,752
14Mar 27, 2024 20:4882,56981,152,25846,893,000997,0002,367,488
15Mar 27, 2024 20:4882,57479,500,28345,898,0001,995,0002,285,568
16Mar 27, 2024 20:4882,99772,246,79047,136,0001,002,0002,285,568
17Mar 27, 2024 20:4883,34375,171,60548,339,00002,285,568
18Mar 27, 2024 20:4883,67475,074,38747,520,0001,011,0002,285,568
19Mar 27, 2024 20:4884,150114,029,38548,806,73002,228,224
20Mar 27, 2024 20:4884,452117,166,27448,982,33802,228,224
21Mar 27, 2024 20:4884,490119,494,24749,004,31302,224,128
22Mar 27, 2024 20:4884,900117,221,61749,241,90702,232,320
23Mar 27, 2024 20:4884,931112,027,88649,260,21902,215,936
24Mar 27, 2024 20:4885,451112,769,47649,561,46402,228,224