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 listApr 13, 2024 13:43E SequeiraE SequeiraScore: 77,395Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 13, 2024 13:4476,07169,250,74142,116,0002,005,0002,510,848
2Apr 13, 2024 13:4476,12965,295,25643,152,0001,003,0002,285,568
3Apr 13, 2024 13:4476,30270,002,17842,244,0002,011,0002,285,568
4Apr 13, 2024 13:4376,31968,976,98642,253,0002,012,0002,285,568
5Apr 13, 2024 13:4376,36268,977,25343,284,0001,006,0002,285,568
6Apr 13, 2024 13:4476,56270,296,21143,397,0001,009,0002,285,568
7Apr 13, 2024 13:4476,57268,840,97643,403,0001,009,0002,285,568
8Apr 13, 2024 13:4376,68165,741,34943,465,0001,010,0002,363,392
9Apr 13, 2024 13:4476,69071,491,42143,470,0001,010,0002,285,568
10Apr 13, 2024 13:4476,83167,018,27243,550,0001,012,0002,285,568
11Apr 13, 2024 13:4477,18475,622,96943,750,0001,017,0002,510,848
12Apr 13, 2024 13:4377,32473,324,01144,848,00002,285,568
13Apr 13, 2024 13:4377,39573,858,97043,892,000997,0002,285,568
14Apr 13, 2024 13:4377,76069,534,40443,097,0002,004,0002,510,848
15Apr 13, 2024 13:4377,77672,058,78444,108,0001,002,0002,510,848
16Apr 13, 2024 13:4478,03169,625,19043,247,0002,011,0002,260,992
17Apr 13, 2024 13:4378,33671,434,48545,435,00002,285,568
18Apr 13, 2024 13:4378,33671,240,64544,426,0001,009,0002,285,568
19Apr 13, 2024 13:4480,037103,451,87646,421,70102,228,224
20Apr 13, 2024 13:4480,360135,717,17146,609,01702,240,512
21Apr 13, 2024 13:4380,401108,220,59846,632,75302,240,512
22Apr 13, 2024 13:4380,971110,979,74146,963,28202,240,512
23Apr 13, 2024 13:4380,981100,041,33046,968,72902,232,320
24Apr 13, 2024 13:4481,301108,064,29447,154,46802,232,320