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 20, 2024 14:15E SequeiraE SequeiraScore: 75,953Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 20, 2024 14:1675,13167,824,02042,563,0001,013,0002,260,992
2Apr 20, 2024 14:1575,20371,453,92342,604,0001,014,0002,506,752
3Apr 20, 2024 14:1575,21770,520,72542,612,0001,014,0002,519,040
4Apr 20, 2024 14:1675,24373,976,48642,627,0001,014,0002,519,040
5Apr 20, 2024 14:1575,32674,275,79342,673,0001,016,0002,519,040
6Apr 20, 2024 14:1575,33370,237,07842,677,0001,016,0002,285,568
7Apr 20, 2024 14:1675,38871,160,61942,709,0001,016,0002,519,040
8Apr 20, 2024 14:1575,40276,170,42742,716,0001,017,0002,285,568
9Apr 20, 2024 14:1575,63473,723,21141,874,0001,994,0002,285,568
10Apr 20, 2024 14:1575,65070,779,16441,883,0001,994,0002,285,568
11Apr 20, 2024 14:1675,70271,318,15842,910,000997,0002,269,184
12Apr 20, 2024 14:1675,86269,587,17442,000,0002,000,0002,269,184
13Apr 20, 2024 14:1675,95372,476,08242,051,0002,002,0002,285,568
14Apr 20, 2024 14:1676,06469,178,21142,112,0002,005,0002,285,568
15Apr 20, 2024 14:1576,25270,353,05842,216,0002,010,0002,285,568
16Apr 20, 2024 14:1676,32967,428,31342,259,0002,012,0002,519,040
17Apr 20, 2024 14:1676,47271,921,16142,338,0002,016,0002,269,184
18Apr 20, 2024 14:1577,14069,432,77943,725,0001,016,0002,285,568
19Apr 20, 2024 14:1578,507114,354,71045,533,95502,236,416
20Apr 20, 2024 14:1679,505115,702,19546,113,05302,240,512
21Apr 20, 2024 14:1679,545117,481,46146,135,96102,236,416
22Apr 20, 2024 14:1579,825109,444,27446,298,39402,224,128
23Apr 20, 2024 14:1679,894114,753,32646,338,71302,228,224
24Apr 20, 2024 14:1579,913105,856,78546,349,44302,220,032