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 31, 2024 13:52E SequeiraE SequeiraScore: 76,997Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 31, 2024 14:1075,73471,946,91341,930,0001,996,0002,510,848
2Mar 31, 2024 13:5675,77165,415,97341,950,0001,997,0002,510,848
3Mar 31, 2024 13:5675,79864,205,69141,965,0001,998,0002,367,488
4Mar 31, 2024 13:5275,82671,293,12241,980,0001,999,0002,269,184
5Mar 31, 2024 14:1075,85372,681,23641,996,0001,999,0002,519,040
6Mar 31, 2024 14:1076,00278,895,35743,080,0001,001,0002,510,848
7Mar 31, 2024 13:5676,10269,579,39642,133,0002,006,0002,285,568
8Mar 31, 2024 14:1076,15370,130,20242,162,0002,007,0002,519,040
9Mar 31, 2024 13:5676,18465,378,25942,179,0002,008,0002,285,568
10Mar 31, 2024 13:5276,21065,509,64342,193,0002,009,0002,265,088
11Mar 31, 2024 13:5276,34365,439,39942,267,0002,012,0002,506,752
12Mar 31, 2024 14:1076,34367,755,48842,267,0002,012,0002,387,968
13Mar 31, 2024 13:5276,34865,936,75543,276,0001,006,0002,269,184
14Mar 31, 2024 13:5276,42972,128,24843,322,0001,007,0002,510,848
15Mar 31, 2024 13:5676,49865,796,17843,361,0001,008,0002,285,568
16Mar 31, 2024 13:5676,58669,672,71144,420,00002,285,568
17Mar 31, 2024 13:5676,75767,364,65743,508,0001,011,0002,285,568
18Mar 31, 2024 14:1076,80773,141,88643,536,0001,012,0002,506,752
19Mar 31, 2024 13:5276,99772,687,70743,644,0001,014,0002,285,568
20Mar 31, 2024 13:5277,00271,204,57144,661,00002,285,568
21Mar 31, 2024 14:1077,06667,778,93543,683,0001,015,0002,510,848
22Mar 31, 2024 13:5677,12473,176,12543,716,0001,016,0002,285,568
23Mar 31, 2024 13:5277,38374,185,43642,888,0001,994,0002,510,848
24Mar 31, 2024 13:5277,39570,876,42842,894,0001,995,0002,519,040
25Mar 31, 2024 14:1077,39868,049,36143,894,000997,0002,265,088
26Mar 31, 2024 14:1077,54766,323,54643,978,000999,0002,519,040
27Mar 31, 2024 13:5677,69068,162,58444,059,0001,001,0002,510,848
28Mar 31, 2024 13:5279,185107,909,93145,927,33102,232,320
29Mar 31, 2024 14:1079,371118,764,70446,035,39202,232,320
30Mar 31, 2024 13:5279,719108,022,62646,236,95602,220,032
31Mar 31, 2024 13:5280,184122,686,65746,506,50602,232,320
32Mar 31, 2024 13:5680,568109,740,51246,729,54002,224,128
33Mar 31, 2024 13:5680,866111,567,42746,902,09502,232,320
34Mar 31, 2024 14:1080,932127,130,66846,940,67202,224,128
35Mar 31, 2024 13:5681,216107,240,78847,105,25802,244,608
36Mar 31, 2024 14:1081,608106,176,24447,332,62002,228,224