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:15E SequeiraE SequeiraScore: 79,228Success
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:1575,15765,032,54942,578,0001,013,0002,510,848
2Mar 26, 2024 21:1575,26964,372,31543,656,00002,273,280
3Mar 26, 2024 21:1575,68369,633,61041,901,0001,995,0002,293,760
4Mar 26, 2024 21:1575,69766,127,80741,909,0001,995,0002,510,848
5Mar 26, 2024 21:1575,79874,249,55242,964,000999,0002,523,136
6Mar 27, 2024 09:2475,86971,947,60742,004,0002,000,0002,277,376
7Mar 26, 2024 21:1575,88181,813,36442,011,0002,000,0002,519,040
8Mar 27, 2024 09:2475,88671,261,23643,014,0001,000,0002,285,568
9Mar 26, 2024 21:1575,91071,815,98142,027,0002,001,0002,281,472
10Mar 26, 2024 21:1575,99368,214,18942,073,0002,003,0002,289,664
11Mar 26, 2024 21:1576,08369,216,37642,123,0002,005,0002,523,136
12Mar 26, 2024 21:1576,11071,460,73642,138,0002,006,0002,281,472
13Mar 27, 2024 09:2476,12470,726,94342,146,0002,006,0002,289,664
14Mar 26, 2024 21:1576,21769,972,32343,202,0001,004,0002,510,848
15Mar 26, 2024 21:1576,72472,542,95543,489,0001,011,0002,281,472
16Mar 26, 2024 21:1578,71976,130,32745,657,00002,273,280
17Mar 26, 2024 21:1578,72874,052,00444,648,0001,014,0002,285,568
18Mar 26, 2024 21:1578,85270,556,97444,718,0001,016,0002,506,752
19Mar 27, 2024 09:2478,90572,752,86444,748,0001,017,0002,510,848
20Mar 26, 2024 21:1579,12272,291,74344,894,000997,0002,260,992
21Mar 26, 2024 21:1579,13377,263,02543,902,0001,995,0002,285,568
22Mar 27, 2024 09:2479,13371,805,42843,902,0001,995,0002,510,848
23Mar 26, 2024 21:1579,19569,734,40943,936,0001,997,0002,519,040
24Mar 26, 2024 21:1579,20771,554,94544,942,000998,0002,510,848
25Mar 26, 2024 21:1579,20976,052,10844,943,000998,0002,510,848
26Mar 26, 2024 21:1579,22869,628,91843,955,0001,997,0002,510,848
27Mar 27, 2024 09:2479,26469,816,20343,975,0001,998,0002,285,568
28Mar 26, 2024 21:1579,39370,547,95744,046,0002,002,0002,265,088
29Mar 26, 2024 21:1579,41971,449,54744,061,0002,002,0002,285,568
30Mar 27, 2024 09:2479,41972,021,20945,062,0001,001,0002,519,040
31Mar 27, 2024 09:2479,42470,517,52144,064,0002,002,0002,519,040
32Mar 27, 2024 09:2479,46268,965,08944,085,0002,003,0002,285,568
33Mar 26, 2024 21:1579,48371,528,18444,096,0002,004,0002,260,992
34Mar 26, 2024 21:1579,56469,793,79645,144,0001,003,0002,285,568
35Mar 27, 2024 09:2479,56668,693,97344,142,0002,006,0002,260,992
36Mar 26, 2024 21:1579,58370,235,16944,152,0002,006,0002,285,568
37Mar 26, 2024 21:1579,74169,767,01344,240,0002,010,0002,285,568
38Mar 27, 2024 09:2479,79071,795,84544,266,0002,012,0002,510,848
39Mar 26, 2024 21:1579,89375,002,20044,324,0002,014,0002,285,568
40Mar 27, 2024 09:2479,99768,799,82645,390,0001,008,0002,285,568
41Mar 26, 2024 21:1580,00769,590,20245,396,0001,008,0002,285,568
42Mar 26, 2024 21:1580,16079,116,50745,483,0001,010,0002,519,040
43Mar 27, 2024 09:2482,235102,998,43247,696,26402,228,224
44Mar 26, 2024 21:1582,545104,756,35647,876,32802,236,416
45Mar 27, 2024 09:2482,675129,468,29847,951,39402,228,224
46Mar 26, 2024 21:1582,959119,164,35048,116,28602,224,128
47Mar 26, 2024 21:1583,219112,036,56948,267,03602,240,512
48Mar 27, 2024 09:2483,345115,321,28648,340,27602,236,416
49Mar 26, 2024 21:1583,635109,218,23348,508,35302,228,224
50Mar 26, 2024 21:1583,845123,967,47248,630,36902,224,128
51Mar 26, 2024 21:1584,261107,458,17148,871,40702,232,320