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:36E SequeiraE SequeiraScore: 76,053Success
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:3674,87265,411,31243,426,00002,285,568
2Apr 19, 2024 08:3674,89169,511,41042,427,0001,010,0002,375,680
3Apr 19, 2024 08:3674,93871,348,39142,454,0001,010,0002,285,568
4Apr 19, 2024 08:3675,25771,987,73042,634,0001,015,0002,285,568
5Apr 19, 2024 08:3675,28369,503,54342,649,0001,015,0002,355,200
6Apr 19, 2024 08:3675,62669,762,31342,867,000996,0002,260,992
7Apr 19, 2024 08:3675,66677,652,12241,892,0001,994,0002,506,752
8Apr 19, 2024 08:3675,67974,811,04143,894,00002,285,568
9Apr 19, 2024 08:3675,69572,942,51841,908,0001,995,0002,510,848
10Apr 19, 2024 08:3675,69774,061,26841,909,0001,995,0002,285,568
11Apr 19, 2024 08:3675,73470,144,86441,930,0001,996,0002,285,568
12Apr 19, 2024 08:3675,85370,933,70042,996,000999,0002,285,568
13Apr 19, 2024 08:3676,05374,127,67043,109,0001,002,0002,285,568
14Apr 19, 2024 08:3676,53471,995,70443,382,0001,008,0002,285,568
15Apr 19, 2024 08:3676,82169,076,10043,544,0001,012,0002,510,848
16Apr 19, 2024 08:3676,94367,738,67643,613,0001,014,0002,285,568
17Apr 19, 2024 08:3677,03375,501,07743,664,0001,015,0002,273,280
18Apr 19, 2024 08:3677,38375,156,26642,888,0001,994,0002,265,088
19Apr 19, 2024 08:3678,752115,990,88845,676,23302,232,320
20Apr 19, 2024 08:3679,078105,460,87345,864,95702,232,320
21Apr 19, 2024 08:3679,179109,087,92845,923,88002,228,224
22Apr 19, 2024 08:3679,349102,092,92046,022,60902,228,224
23Apr 19, 2024 08:3679,738114,776,25046,247,88702,224,128
24Apr 19, 2024 08:3680,061110,871,13646,435,42602,232,320