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:17E SequeiraE SequeiraScore: 78,079Success
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:1976,74368,213,71043,500,0001,011,0002,285,568
2Apr 19, 2024 08:1776,82465,958,71843,546,0001,012,0002,285,568
3Apr 19, 2024 08:1977,38374,668,78642,888,0001,994,0002,273,280
4Apr 19, 2024 08:1777,39775,160,35842,895,0001,995,0002,285,568
5Apr 19, 2024 08:1777,41674,904,16542,906,0001,995,0002,285,568
6Apr 19, 2024 08:1977,57468,867,08542,994,0001,999,0002,519,040
7Apr 19, 2024 08:1777,61067,958,02343,014,0002,000,0002,285,568
8Apr 19, 2024 08:1777,70374,462,38043,065,0002,003,0002,359,296
9Apr 19, 2024 08:1977,79870,252,00343,118,0002,005,0002,285,568
10Apr 19, 2024 08:1777,81673,526,35043,128,0002,005,0002,285,568
11Apr 19, 2024 08:1777,99366,670,64343,226,0002,010,0002,519,040
12Apr 19, 2024 08:1978,00275,641,17244,258,000983,0002,285,568
13Apr 19, 2024 08:1978,07968,661,55644,280,0001,006,0002,285,568
14Apr 19, 2024 08:1978,18866,670,46844,342,0001,007,0002,519,040
15Apr 19, 2024 08:1778,27176,267,26344,389,0001,008,0002,273,280
16Apr 19, 2024 08:1778,27670,732,39944,392,0001,008,0002,285,568
17Apr 19, 2024 08:1978,35774,589,25744,438,0001,009,0002,285,568
18Apr 19, 2024 08:1978,43665,546,27144,483,0001,010,0002,269,184
19Apr 19, 2024 08:1979,412106,545,58746,058,96902,240,512
20Apr 19, 2024 08:1780,147108,076,54146,485,28502,232,320
21Apr 19, 2024 08:1980,317117,889,61946,583,87002,240,512
22Apr 19, 2024 08:1780,454107,386,45946,663,39502,232,320
23Apr 19, 2024 08:1781,069110,857,21847,020,05902,228,224
24Apr 19, 2024 08:1981,309121,264,18447,159,43802,224,128