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 11, 2024 04:21Tejas G.Tejas G.Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 11, 2024 04:2100000Error: expected "3105022 ", got "3122660 "
2Feb 11, 2024 04:2155,89756,673,69231,407,0001,013,0002,285,568
3Feb 11, 2024 04:2255,90555,122,08031,412,0001,013,0002,285,568
4Feb 11, 2024 04:2256,061104,340,25132,515,44902,252,800
5Feb 11, 2024 04:2156,38587,987,12332,703,02602,248,704
6Feb 11, 2024 04:2156,70357,952,56430,895,0001,993,0002,269,184
7Feb 11, 2024 04:2256,851101,840,21232,973,60502,248,704
8Feb 11, 2024 04:2157,053108,527,12133,090,52902,244,608
9Feb 11, 2024 04:2157,34154,670,95231,243,0002,015,0002,285,568
10Feb 11, 2024 04:2257,67257,422,69532,437,0001,013,0002,285,568
11Feb 11, 2024 04:2158,22258,024,52932,776,000993,0002,285,568
12Feb 11, 2024 04:2258,42261,957,75531,892,0001,993,0002,285,568
13Feb 11, 2024 04:2158,46964,576,27232,915,000997,0002,277,376
14Feb 11, 2024 04:2158,61097,857,85233,993,76302,252,800
15Feb 11, 2024 04:2158,66755,753,56132,026,0002,001,0002,400,256
16Feb 11, 2024 04:2258,82256,237,52732,111,0002,006,0002,277,376
17Feb 11, 2024 04:2258,83356,373,02532,116,0002,007,0002,371,584
18Feb 11, 2024 04:2259,06398,465,45534,256,61502,240,512
19Feb 11, 2024 04:2159,10757,275,44632,266,0002,016,0002,285,568
20Feb 11, 2024 04:2259,28658,435,27033,375,0001,011,0002,269,184
21Feb 11, 2024 04:2259,31056,058,59433,389,0001,011,0002,285,568
22Feb 11, 2024 04:2259,58356,839,55533,542,0001,016,0002,277,376
23Feb 11, 2024 04:2259,58855,676,89534,561,00002,285,568
24Feb 11, 2024 04:2160,21259,258,64633,926,000997,0002,523,136
25Feb 11, 2024 04:2160,25360,936,19932,950,0001,997,0002,273,280