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:30E SequeiraE SequeiraScore: 76,726Success
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:3075,02468,489,26643,514,00002,285,568
2Apr 19, 2024 08:3075,08467,739,36042,537,0001,012,0002,519,040
3Apr 19, 2024 08:3175,25071,776,69442,630,0001,015,0002,519,040
4Apr 19, 2024 08:3175,34572,081,19442,684,0001,016,0002,285,568
5Apr 19, 2024 08:3175,35274,584,99042,688,0001,016,0002,273,280
6Apr 19, 2024 08:3075,41976,675,90342,726,0001,017,0002,285,568
7Apr 19, 2024 08:3075,66273,749,99041,890,0001,994,0002,510,848
8Apr 19, 2024 08:3175,67969,900,07841,899,0001,995,0002,285,568
9Apr 19, 2024 08:3075,73667,754,73941,931,0001,996,0002,277,376
10Apr 19, 2024 08:3075,86274,707,42642,000,0002,000,0002,510,848
11Apr 19, 2024 08:3176,39766,162,67743,303,0001,007,0002,519,040
12Apr 19, 2024 08:3176,51667,708,96843,371,0001,008,0002,510,848
13Apr 19, 2024 08:3076,72667,675,15444,501,00002,510,848
14Apr 19, 2024 08:3076,75971,325,56343,509,0001,011,0002,285,568
15Apr 19, 2024 08:3176,81467,588,91544,552,00002,519,040
16Apr 19, 2024 08:3176,93467,825,38743,608,0001,014,0002,285,568
17Apr 19, 2024 08:3177,15374,175,58444,749,00002,285,568
18Apr 19, 2024 08:3077,39176,171,30743,890,000997,0002,285,568
19Apr 19, 2024 08:3078,594115,290,94745,584,63002,240,512
20Apr 19, 2024 08:3078,607119,085,26945,592,22802,236,416
21Apr 19, 2024 08:3078,876107,414,74545,748,24102,232,320
22Apr 19, 2024 08:3178,880119,815,02145,750,22402,224,128
23Apr 19, 2024 08:3178,953133,363,95845,792,54502,252,800
24Apr 19, 2024 08:3179,15397,079,44645,908,56902,228,224