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 14, 2024 10:17E SequeiraE SequeiraScore: 79,585Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 14, 2024 10:1778,62170,054,65744,587,0001,013,0002,510,848
2Apr 14, 2024 10:1778,73173,563,14445,664,00002,285,568
3Apr 14, 2024 10:1778,81678,160,19145,713,00002,519,040
4Apr 14, 2024 10:1779,18872,589,93544,931,000998,0002,277,376
5Apr 14, 2024 10:1779,32671,157,89044,009,0002,000,0002,519,040
6Apr 14, 2024 10:1779,54377,674,45245,133,0001,002,0002,285,568
7Apr 14, 2024 10:1779,58598,145,15146,159,20802,220,032
8Apr 14, 2024 10:1780,13468,575,72145,468,0001,010,0002,506,752
9Apr 14, 2024 10:1780,140110,478,85246,481,08902,224,128
10Apr 14, 2024 10:1780,48476,708,71345,667,0001,014,0002,519,040
11Apr 14, 2024 10:1781,283103,960,22047,144,02202,232,320
12Apr 14, 2024 10:1781,74170,050,62146,402,0001,008,0002,285,568