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:11E SequeiraE SequeiraScore: 80,214Success
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:1075,90966,226,29042,026,0002,001,0002,293,760
2Mar 26, 2024 21:1176,11077,331,70842,138,0002,006,0002,281,472
3Mar 26, 2024 21:1076,49076,671,31942,348,0002,016,0002,506,752
4Mar 26, 2024 21:1076,52270,828,24742,366,0002,017,0002,510,848
5Mar 26, 2024 21:1276,53866,811,25343,384,0001,008,0002,273,280
6Mar 26, 2024 21:1276,55568,742,19643,393,0001,009,0002,281,472
7Mar 26, 2024 21:1176,58368,025,62244,418,00002,510,848
8Mar 26, 2024 21:1276,98673,808,60844,652,00002,506,752
9Mar 26, 2024 21:1177,01274,615,16744,667,00002,506,752
10Mar 26, 2024 21:1279,46966,672,83144,088,0002,004,0002,285,568
11Mar 26, 2024 21:1279,51069,882,92144,111,0002,005,0002,285,568
12Mar 26, 2024 21:1079,52969,102,21944,122,0002,005,0002,347,008
13Mar 26, 2024 21:1279,64868,982,35445,192,0001,004,0002,285,568
14Mar 26, 2024 21:1179,68370,235,05044,207,0002,009,0002,285,568
15Mar 26, 2024 21:1179,68367,903,95544,207,0002,009,0002,510,848
16Mar 26, 2024 21:1079,74868,632,66944,243,0002,011,0002,506,752
17Mar 26, 2024 21:1179,98370,548,52145,382,0001,008,0002,285,568
18Mar 26, 2024 21:1079,99872,324,24345,391,0001,008,0002,285,568
19Mar 26, 2024 21:1080,00269,103,32646,401,00002,510,848
20Mar 26, 2024 21:1280,00577,434,17745,395,0001,008,0002,285,568
21Mar 26, 2024 21:1080,11068,620,66745,454,0001,010,0002,285,568
22Mar 26, 2024 21:1280,11472,392,31445,456,0001,010,0002,510,848
23Mar 26, 2024 21:1280,21471,246,36946,524,00002,269,184
24Mar 26, 2024 21:1080,25269,396,58145,535,0001,011,0002,506,752
25Mar 26, 2024 21:1280,28869,349,68945,555,0001,012,0002,285,568
26Mar 26, 2024 21:1180,35770,224,15945,594,0001,013,0002,285,568
27Mar 26, 2024 21:1180,45771,035,50145,651,0001,014,0002,285,568
28Mar 26, 2024 21:1180,57271,539,89845,717,0001,015,0002,285,568
29Mar 26, 2024 21:1080,59378,374,28045,728,0001,016,0002,506,752
30Mar 26, 2024 21:1280,80273,689,62845,868,000997,0002,510,848
31Mar 26, 2024 21:1080,82672,396,62245,882,000997,0002,273,280
32Mar 26, 2024 21:1180,83872,252,53544,891,0001,995,0002,285,568
33Mar 26, 2024 21:1080,96470,275,49545,960,000999,0002,285,568
34Mar 26, 2024 21:1181,05970,868,56545,014,0002,000,0002,510,848
35Mar 26, 2024 21:1281,09368,481,66045,033,0002,001,0002,347,008
36Mar 26, 2024 21:1181,20571,743,94945,095,0002,004,0002,506,752
37Mar 26, 2024 21:1081,429112,045,18447,228,92102,224,128
38Mar 26, 2024 21:1281,458122,080,55847,245,35502,228,224
39Mar 26, 2024 21:1081,759101,657,61447,419,96402,228,224
40Mar 26, 2024 21:1281,955127,256,37447,533,90802,232,320
41Mar 26, 2024 21:1182,235111,458,29147,696,24202,228,224
42Mar 26, 2024 21:1282,303116,112,94247,735,93702,224,128
43Mar 26, 2024 21:1182,531111,112,50147,868,22302,232,320
44Mar 26, 2024 21:1082,572103,668,41747,891,76902,220,032
45Mar 26, 2024 21:1182,899110,201,97748,081,17802,228,224