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 27, 2024 13:09E SequeiraE SequeiraScore: 76,598Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 13:0975,66774,571,80441,893,0001,994,0002,506,752
2Mar 27, 2024 13:0975,68874,340,99841,904,0001,995,0002,519,040
3Mar 27, 2024 13:0975,72677,599,32642,923,000998,0002,285,568
4Mar 27, 2024 13:0975,76267,345,20441,945,0001,997,0002,510,848
5Mar 27, 2024 13:1075,76769,552,87741,948,0001,997,0002,510,848
6Mar 27, 2024 13:1075,79866,482,05141,965,0001,998,0002,285,568
7Mar 27, 2024 13:1075,84066,714,78742,988,000999,0002,273,280
8Mar 27, 2024 13:0975,88669,194,86842,014,0002,000,0002,285,568
9Mar 27, 2024 13:1076,03876,540,76442,098,0002,004,0002,285,568
10Mar 27, 2024 13:0976,06468,011,18942,112,0002,005,0002,260,992
11Mar 27, 2024 13:1076,09372,117,87742,128,0002,006,0002,285,568
12Mar 27, 2024 13:0976,12466,488,95142,146,0002,006,0002,510,848
13Mar 27, 2024 13:1076,21471,175,77042,195,0002,009,0002,510,848
14Mar 27, 2024 13:0976,33466,827,61442,262,0002,012,0002,285,568
15Mar 27, 2024 13:0976,49768,017,95444,368,00002,285,568
16Mar 27, 2024 13:0976,50768,401,22243,366,0001,008,0002,519,040
17Mar 27, 2024 13:0976,50965,719,14844,375,00002,260,992
18Mar 27, 2024 13:0976,57673,998,33243,405,0001,009,0002,519,040
19Mar 27, 2024 13:0976,59866,503,95343,418,0001,009,0002,510,848
20Mar 27, 2024 13:1076,62167,315,26043,430,0001,010,0002,273,280
21Mar 27, 2024 13:1076,90772,607,61743,593,0001,013,0002,285,568
22Mar 27, 2024 13:0977,08170,690,49844,707,00002,510,848
23Mar 27, 2024 13:0977,09876,157,26043,701,0001,016,0002,269,184
24Mar 27, 2024 13:1077,17471,949,15843,744,0001,017,0002,285,568
25Mar 27, 2024 13:0977,31771,178,88043,848,000996,0002,285,568
26Mar 27, 2024 13:0977,43472,775,42542,916,0001,996,0002,285,568
27Mar 27, 2024 13:0977,68170,724,62743,053,0002,002,0002,285,568
28Mar 27, 2024 13:0980,203114,380,88746,517,75602,232,320
29Mar 27, 2024 13:0980,446110,389,82846,658,53402,232,320
30Mar 27, 2024 13:0980,475115,619,67846,675,48102,232,320
31Mar 27, 2024 13:1080,861108,986,99146,899,59402,224,128
32Mar 27, 2024 13:0980,871105,497,97746,904,94402,232,320
33Mar 27, 2024 13:1080,927100,481,97646,937,41002,232,320
34Mar 27, 2024 13:0980,999103,170,53646,979,66502,236,416
35Mar 27, 2024 13:0981,008112,933,12346,984,72802,224,128
36Mar 27, 2024 13:1081,688107,099,25047,379,10902,228,224