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 listMar 27, 2024 13:02E SequeiraE SequeiraScore: 77,755Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 13:0276,38666,360,65642,291,0002,013,0002,285,568
2Mar 27, 2024 13:0276,50571,137,90643,365,0001,008,0002,510,848
3Mar 27, 2024 13:0276,51969,405,60943,373,0001,008,0002,285,568
4Mar 27, 2024 13:0276,82970,131,82844,561,00002,285,568
5Mar 27, 2024 13:0276,98474,862,89043,637,0001,014,0002,510,848
6Mar 27, 2024 13:0277,05075,972,36843,674,0001,015,0002,363,392
7Mar 27, 2024 13:0277,32273,525,53542,854,0001,993,0002,285,568
8Mar 27, 2024 13:0277,36070,756,17242,875,0001,994,0002,285,568
9Mar 27, 2024 13:0277,38671,310,94142,890,0001,994,0002,510,848
10Mar 27, 2024 13:0277,38871,794,51743,888,000997,0002,273,280
11Mar 27, 2024 13:0277,44768,404,07642,923,0001,996,0002,273,280
12Mar 27, 2024 13:0277,56773,677,04542,990,0001,999,0002,269,184
13Mar 27, 2024 13:0277,75568,543,78244,096,0001,002,0002,273,280
14Mar 27, 2024 13:0277,78468,228,82444,113,0001,002,0002,510,848
15Mar 27, 2024 13:0278,18172,667,77745,345,00002,510,848
16Mar 27, 2024 13:0278,26468,918,13444,385,0001,008,0002,265,088
17Mar 27, 2024 13:0278,52467,713,62444,532,0001,012,0002,330,624
18Mar 27, 2024 13:0278,78472,369,09845,695,00002,510,848
19Mar 27, 2024 13:0280,894111,374,28646,918,54902,224,128
20Mar 27, 2024 13:0281,276114,492,34947,139,80502,228,224
21Mar 27, 2024 13:0281,415123,791,69147,220,62402,224,128
22Mar 27, 2024 13:0281,680103,458,12947,374,63002,224,128
23Mar 27, 2024 13:0281,696120,460,87347,383,88402,220,032
24Mar 27, 2024 13:0281,726113,123,66347,400,87002,232,320