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 26, 2024 21:12E SequeiraE SequeiraScore: 80,255Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 26, 2024 21:1275,92873,584,19042,037,0002,001,0002,510,848
2Mar 26, 2024 21:1276,33866,880,22042,264,0002,012,0002,334,720
3Mar 26, 2024 21:1276,42967,441,21043,322,0001,007,0002,285,568
4Mar 26, 2024 21:1276,74866,488,44343,503,0001,011,0002,519,040
5Mar 26, 2024 21:1276,86273,901,87042,599,0001,981,0002,293,760
6Mar 26, 2024 21:1276,91970,202,00343,600,0001,013,0002,510,848
7Mar 26, 2024 21:1279,27667,073,51743,981,0001,999,0002,371,584
8Mar 26, 2024 21:1279,31671,218,49044,003,0002,000,0002,285,568
9Mar 26, 2024 21:1279,49869,959,59744,105,0002,004,0002,363,392
10Mar 26, 2024 21:1279,55769,936,82044,137,0002,006,0002,510,848
11Mar 26, 2024 21:1279,63466,473,83244,180,0002,008,0002,285,568
12Mar 26, 2024 21:1279,65269,507,61744,190,0002,008,0002,285,568
13Mar 26, 2024 21:1279,74068,510,57744,239,0002,010,0002,285,568
14Mar 26, 2024 21:1280,11467,337,26545,456,0001,010,0002,285,568
15Mar 26, 2024 21:1280,16072,365,93145,483,0001,010,0002,375,680
16Mar 26, 2024 21:1280,25572,128,00645,537,0001,011,0002,506,752
17Mar 26, 2024 21:1280,26072,109,11646,551,00002,285,568
18Mar 26, 2024 21:1280,31468,459,52545,570,0001,012,0002,285,568
19Mar 26, 2024 21:1280,32876,571,50046,590,00002,519,040
20Mar 26, 2024 21:1280,57675,996,36645,719,0001,015,0002,510,848
21Mar 26, 2024 21:1280,82673,300,21445,882,000997,0002,285,568
22Mar 26, 2024 21:1280,83477,969,33144,889,0001,995,0002,285,568
23Mar 26, 2024 21:1280,85076,253,37845,896,000997,0002,510,848
24Mar 26, 2024 21:1280,95069,130,64844,954,0001,997,0002,273,280
25Mar 26, 2024 21:1281,249102,431,98847,124,26702,232,320
26Mar 26, 2024 21:1281,330101,031,74247,171,41902,224,128
27Mar 26, 2024 21:1281,428112,917,15447,228,15002,236,416
28Mar 26, 2024 21:1281,538118,186,77747,292,05302,224,128
29Mar 26, 2024 21:1281,972103,091,74447,544,00602,228,224
30Mar 26, 2024 21:1283,127111,956,73948,213,77902,228,224