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 listFeb 16, 2022 03:40Istomin MikhailIstomin MikhailScore: 87,047Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 18, 2022 05:4164,20366,389,75235,226,0002,012,0002,486,272
2Feb 16, 2022 03:3365,02962,970,63536,698,0001,019,0002,285,568
3Feb 16, 2022 03:4067,36765,021,60337,070,0002,003,0002,285,568
4Feb 18, 2022 05:4167,85263,868,92837,336,0002,018,0002,285,568
5Feb 16, 2022 03:4068,37165,025,91038,639,0001,016,0002,482,176
6Feb 16, 2022 03:3369,10562,405,84138,077,0002,004,0002,469,888
7Feb 18, 2022 05:4172,20367,094,33139,884,0001,994,0002,469,888
8Feb 16, 2022 03:3376,12972,661,15441,145,0003,010,0002,461,696
9Feb 16, 2022 03:4077,84869,893,77741,139,0004,013,0002,527,232
10Feb 16, 2022 03:4077,96067,834,54542,203,0003,014,0002,473,984
11Feb 16, 2022 03:4082,82168,700,62744,033,0004,003,0002,281,472
12Feb 16, 2022 03:4082,98873,786,50146,128,0002,005,0002,478,080
13Feb 18, 2022 05:4184,26676,558,98044,885,0003,989,0002,465,792
14Feb 16, 2022 03:3384,26975,138,95645,884,0002,992,0002,461,696
15Feb 18, 2022 05:4184,947110,241,33249,269,09202,179,072
16Feb 16, 2022 03:3385,715112,656,50649,714,60402,191,360
17Feb 16, 2022 03:3385,835108,898,80249,784,50502,301,952
18Feb 16, 2022 03:3385,95281,976,72947,858,0001,994,0002,306,048
19Feb 18, 2022 05:4185,98472,660,46546,879,0002,992,0002,461,696
20Feb 18, 2022 05:4186,112117,861,56849,945,14702,183,168
21Feb 18, 2022 05:4186,66273,835,51148,254,0002,010,0002,506,752
22Feb 18, 2022 05:4187,04773,257,01546,448,0004,039,0002,453,504
23Feb 16, 2022 03:3387,36481,031,58947,631,0003,040,0002,289,664
24Feb 16, 2022 03:3387,70376,040,12446,879,0003,989,0002,449,408
25Feb 16, 2022 03:3387,88878,198,56346,977,0003,998,0002,285,568
26Feb 18, 2022 05:4188,33874,036,86647,218,0004,018,0002,289,664
27Feb 16, 2022 03:3388,63180,099,86348,383,0003,023,0002,482,176
28Feb 16, 2022 03:3389,31977,942,96948,817,0002,988,0002,314,240
29Feb 18, 2022 05:4189,778121,343,75152,071,21502,179,072
30Feb 18, 2022 05:4190,21674,333,23148,300,0004,025,0002,453,504
31Feb 16, 2022 03:4090,951115,384,51152,751,30302,183,168
32Feb 16, 2022 03:4094,990115,955,28455,094,43802,179,072
33Feb 16, 2022 03:3396,23184,102,15252,824,0002,990,0002,265,088
34Feb 18, 2022 05:4199,86079,604,43053,925,0003,994,0002,445,312
35Feb 18, 2022 05:41102,83191,435,19656,610,0003,032,0002,334,720
36Feb 18, 2022 05:41105,02886,490,32856,922,0003,994,0002,433,024
37Feb 16, 2022 03:40107,081118,699,87262,107,11002,195,456
38Feb 16, 2022 03:40111,556128,241,70264,702,71802,232,320
39Feb 16, 2022 03:33113,987134,520,32766,112,70002,301,952
40Feb 16, 2022 03:40118,651133,209,78468,817,58202,306,048
41Feb 16, 2022 03:40118,920140,853,07268,973,72802,240,512
42Feb 16, 2022 03:33121,51294,195,95268,464,0002,013,0002,437,120