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:31E SequeiraE SequeiraScore: 76,717Success
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:3175,16666,570,41743,596,00002,359,296
2Apr 19, 2024 08:3175,33171,825,97342,676,0001,016,0002,285,568
3Apr 19, 2024 08:3175,66071,018,05541,889,0001,994,0002,285,568
4Apr 19, 2024 08:3175,88668,491,22642,014,0002,000,0002,285,568
5Apr 19, 2024 08:3176,04767,114,52842,103,0002,004,0002,506,752
6Apr 19, 2024 08:3176,09374,855,08942,128,0002,006,0002,285,568
7Apr 19, 2024 08:3176,10366,837,16042,134,0002,006,0002,285,568
8Apr 19, 2024 08:3176,10566,477,65042,135,0002,006,0002,510,848
9Apr 19, 2024 08:3176,50769,004,08344,374,00002,285,568
10Apr 19, 2024 08:3176,57166,159,76543,402,0001,009,0002,285,568
11Apr 19, 2024 08:3176,59070,800,22343,413,0001,009,0002,510,848
12Apr 19, 2024 08:3176,59365,517,53443,415,0001,009,0002,285,568
13Apr 19, 2024 08:3176,71766,298,74943,485,0001,011,0002,375,680
14Apr 19, 2024 08:3177,07872,740,11544,705,00002,285,568
15Apr 19, 2024 08:3177,08670,298,33544,710,00002,260,992
16Apr 19, 2024 08:3177,40373,257,60142,899,0001,995,0002,285,568
17Apr 19, 2024 08:3177,43372,086,96943,913,000998,0002,285,568
18Apr 19, 2024 08:3177,73873,157,90743,085,0002,003,0002,510,848
19Apr 19, 2024 08:3179,039119,533,12545,842,60302,236,416
20Apr 19, 2024 08:3179,350100,643,92146,023,17002,224,128
21Apr 19, 2024 08:3179,391106,809,85446,046,96102,232,320
22Apr 19, 2024 08:3179,54398,492,66446,135,04402,228,224
23Apr 19, 2024 08:3179,974105,914,85346,384,63602,232,320
24Apr 19, 2024 08:3179,978111,043,64046,387,07302,240,512