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 19, 2024 08:22E SequeiraE SequeiraScore: 76,860Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 19, 2024 08:2275,81774,284,15041,976,0001,998,0002,519,040
2Apr 19, 2024 08:2275,84372,966,47641,990,0001,999,0002,285,568
3Apr 19, 2024 08:2475,85362,163,01941,996,0001,999,0002,510,848
4Apr 19, 2024 08:2275,87171,170,30642,005,0002,000,0002,506,752
5Apr 19, 2024 08:2475,91064,235,40442,027,0002,001,0002,351,104
6Apr 19, 2024 08:2475,96668,571,02442,058,0002,002,0002,510,848
7Apr 19, 2024 08:2276,06766,532,09342,114,0002,005,0002,285,568
8Apr 19, 2024 08:2276,29073,150,44242,237,0002,011,0002,273,280
9Apr 19, 2024 08:2276,75065,690,56243,504,0001,011,0002,510,848
10Apr 19, 2024 08:2276,78465,784,24543,523,0001,012,0002,285,568
11Apr 19, 2024 08:2476,79567,211,62443,529,0001,012,0002,363,392
12Apr 19, 2024 08:2276,83366,410,10343,551,0001,012,0002,510,848
13Apr 19, 2024 08:2476,86067,253,41744,579,00002,519,040
14Apr 19, 2024 08:2276,87270,483,78443,573,0001,013,0002,510,848
15Apr 19, 2024 08:2476,92267,090,10744,615,00002,285,568
16Apr 19, 2024 08:2476,96670,320,80843,626,0001,014,0002,285,568
17Apr 19, 2024 08:2477,48368,112,21542,943,0001,997,0002,510,848
18Apr 19, 2024 08:2477,48673,124,59143,944,000998,0002,285,568
19Apr 19, 2024 08:2278,308117,300,20445,418,83202,236,416
20Apr 19, 2024 08:2478,344111,071,62445,439,43002,224,128
21Apr 19, 2024 08:2278,740103,185,72245,669,02202,244,608
22Apr 19, 2024 08:2479,074107,578,39745,863,11502,240,512
23Apr 19, 2024 08:2479,287118,276,61545,986,18102,236,416
24Apr 19, 2024 08:2279,469102,782,22446,091,79002,244,608