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 28, 2024 10:33E SequeiraE SequeiraScore: 77,128Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 28, 2024 10:3375,80567,471,52541,969,0001,998,0002,510,848
2Mar 28, 2024 10:3376,07466,734,42642,118,0002,005,0002,285,568
3Mar 28, 2024 10:3376,11267,399,43942,139,0002,006,0002,285,568
4Mar 28, 2024 10:3376,15968,024,18842,165,0002,007,0002,510,848
5Mar 28, 2024 10:3376,16767,185,62342,169,0002,008,0002,260,992
6Mar 28, 2024 10:3376,26667,558,22343,229,0001,005,0002,510,848
7Mar 28, 2024 10:3376,28367,170,71943,239,0001,005,0002,519,040
8Mar 28, 2024 10:3376,43366,862,44643,324,0001,007,0002,285,568
9Mar 28, 2024 10:3376,44167,796,31642,321,0002,015,0002,285,568
10Mar 28, 2024 10:3376,79866,761,21243,531,0001,012,0002,285,568
11Mar 28, 2024 10:3377,00774,347,02644,664,00002,494,464
12Mar 28, 2024 10:3377,06070,494,74443,680,0001,015,0002,285,568
13Mar 28, 2024 10:3377,12873,093,56843,718,0001,016,0002,273,280
14Mar 28, 2024 10:3377,40075,357,94743,895,000997,0002,285,568
15Mar 28, 2024 10:3377,40978,813,63642,902,0001,995,0002,519,040
16Mar 28, 2024 10:3377,55367,824,93743,982,000999,0002,510,848
17Mar 28, 2024 10:3377,57667,439,33342,995,0001,999,0002,273,280
18Mar 28, 2024 10:3377,64170,319,49544,032,0001,000,0002,510,848
19Mar 28, 2024 10:3380,549111,420,38746,718,57402,228,224
20Mar 28, 2024 10:3380,583116,178,42046,738,09402,232,320
21Mar 28, 2024 10:3380,713104,415,72746,813,57602,256,896
22Mar 28, 2024 10:3381,419120,410,06747,222,80802,228,224
23Mar 28, 2024 10:3381,563120,217,94047,306,47102,232,320
24Mar 28, 2024 10:3381,731108,479,32247,404,11702,236,416