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 11, 2026 23:55KeefeKeefeScore: 6,543Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 13, 2026 04:485,02919,878,7761,945,000972,0002,273,280
2Mar 13, 2026 04:485,03318,623,460973,0001,946,0002,506,752
3Mar 13, 2026 04:485,03316,559,4131,946,000973,0002,273,280
4Mar 13, 2026 04:485,03418,602,081973,0001,947,0002,330,624
5Mar 13, 2026 04:485,06620,802,1741,959,000979,0002,265,088
6Mar 13, 2026 04:485,06928,352,3891,960,000980,0002,441,216
7Mar 13, 2026 04:485,07221,083,8561,471,0001,471,0002,265,088
8Mar 13, 2026 04:485,16720,002,787999,0001,998,0002,273,280
9Mar 12, 2026 03:535,22620,135,8181,010,0002,021,0002,379,776
10Mar 12, 2026 03:535,23623,438,1562,025,0001,012,0002,265,088
11Mar 13, 2026 04:485,24123,132,3601,013,0002,027,0002,281,472
12Mar 12, 2026 03:535,30918,857,4882,053,0001,026,0002,519,040
13Mar 12, 2026 03:535,35019,370,5702,069,0001,034,0002,273,280
14Mar 12, 2026 03:535,35520,542,6211,035,0002,071,0002,506,752
15Mar 12, 2026 03:535,39021,845,0381,042,0002,084,0002,277,376
16Mar 12, 2026 03:535,41225,684,4502,093,0001,046,0002,412,544
17Mar 12, 2026 03:535,45322,159,5972,109,0001,054,0002,273,280
18Mar 12, 2026 00:385,50920,805,9701,065,0002,130,0002,273,280
19Mar 12, 2026 00:385,52424,030,1992,136,0001,068,0002,375,680
20Mar 12, 2026 03:535,56021,436,1852,150,0001,075,0002,396,160
21Mar 12, 2026 00:385,57219,600,5713,232,00002,338,816
22Mar 12, 2026 00:385,59125,323,4172,162,0001,081,0002,273,280
23Mar 12, 2026 00:385,66920,724,8622,192,0001,096,0002,273,280
24Mar 12, 2026 00:385,67128,155,7743,289,00002,506,752
25Mar 12, 2026 00:345,72820,728,7772,215,0001,107,0002,269,184
26Mar 12, 2026 00:385,74118,512,9732,220,0001,110,0002,510,848
27Mar 12, 2026 00:345,79819,462,2402,242,0001,121,0002,461,696
28Mar 12, 2026 00:385,82924,102,8502,254,0001,127,0002,510,848
29Mar 12, 2026 00:385,85222,124,5732,263,0001,131,0002,510,848
30Mar 12, 2026 00:245,88121,504,4222,274,0001,137,0002,510,848
31Mar 12, 2026 00:245,93817,747,9032,296,0001,148,0002,465,792
32Mar 12, 2026 00:245,94321,967,4463,447,00002,437,120
33Mar 12, 2026 00:245,96619,153,0752,307,0001,153,0002,306,048
34Mar 12, 2026 00:245,97222,253,7043,464,00002,289,664
35Mar 12, 2026 00:245,98621,260,6273,472,00002,273,280
36Mar 12, 2026 00:345,99520,601,3162,318,0001,159,0002,490,368
37Mar 12, 2026 00:246,05923,278,4153,514,00002,273,280
38Mar 12, 2026 00:246,11419,033,1642,364,0001,182,0002,273,280
39Mar 12, 2026 00:246,27625,331,4422,427,0001,213,0002,486,272
40Mar 12, 2026 00:246,30217,660,8742,437,0001,218,0002,400,256
41Mar 12, 2026 00:246,34320,775,8522,453,0001,226,0002,498,560
42Mar 12, 2026 00:246,35924,336,1902,459,0001,229,0002,453,504
43Mar 12, 2026 00:246,41020,654,6382,479,0001,239,0002,383,872
44Mar 12, 2026 00:246,43120,614,4443,730,00002,416,640
45Mar 12, 2026 00:246,49320,090,0883,766,00002,363,392
46Mar 12, 2026 00:246,50223,656,6232,514,0001,257,0002,273,280
47Mar 12, 2026 00:076,54319,511,0452,530,0001,265,0002,301,952
48Mar 12, 2026 00:036,57419,027,1413,813,00002,502,656
49Mar 12, 2026 00:036,59024,228,5202,548,0001,274,0002,494,464
50Mar 12, 2026 00:246,66918,940,6502,579,0001,289,0002,269,184
51Mar 12, 2026 00:246,74120,836,5431,955,0001,955,0002,334,720
52Mar 12, 2026 00:036,75523,924,5642,939,000979,0002,498,560
53Mar 12, 2026 00:036,75521,881,253979,0002,939,0002,260,992
54Mar 12, 2026 00:176,75524,394,2462,939,000979,0002,510,848
55Mar 12, 2026 00:176,75923,040,7161,960,0001,960,0002,330,624
56Mar 12, 2026 00:036,76224,536,8872,942,000980,0002,519,040
57Mar 12, 2026 00:036,76220,664,0011,961,0001,961,0002,437,120
58Mar 12, 2026 00:036,76619,814,9082,943,000981,0002,297,856
59Mar 12, 2026 00:036,76626,731,2192,943,000981,0002,514,944
60Mar 12, 2026 00:036,80022,725,2821,972,0001,972,0002,322,432
61Mar 12, 2026 00:066,83821,161,2613,966,00002,285,568
62Mar 12, 2026 00:176,87919,225,0291,995,0001,995,0002,285,568
63Mar 12, 2026 00:076,92124,092,2092,007,0002,007,0002,281,472
64Mar 12, 2026 00:067,03821,150,0392,041,0002,041,0002,379,776
65Mar 12, 2026 00:077,06226,529,4972,048,0002,048,0002,273,280
66Mar 12, 2026 00:067,17921,096,1993,123,0001,041,0002,506,752
67Mar 12, 2026 00:008,15321,317,3834,729,00002,465,792
68Mar 12, 2026 00:008,22621,237,3794,771,00002,461,696
69Mar 12, 2026 00:008,27123,297,9853,598,0001,199,0002,519,040
70Mar 12, 2026 00:008,28819,271,5134,807,00002,523,136
71Mar 12, 2026 00:008,42424,270,5383,909,000977,0002,510,848
72Mar 12, 2026 00:008,47124,463,6383,931,000982,0002,269,184
73Mar 12, 2026 00:008,47424,964,9913,932,000983,0002,273,280
74Mar 12, 2026 00:008,47424,416,4123,932,000983,0002,269,184
75Mar 12, 2026 00:008,47923,707,0362,951,0001,967,0002,494,464
76Mar 11, 2026 23:598,85921,292,6513,083,0002,055,0002,523,136
77Mar 11, 2026 23:598,86721,151,1243,086,0002,057,0002,510,848
78Mar 11, 2026 23:598,93325,235,0354,145,0001,036,0002,265,088
79Mar 11, 2026 23:598,93629,932,1873,110,0002,073,0002,285,568
80Mar 11, 2026 23:598,94022,412,9763,111,0002,074,0002,498,560
81Mar 11, 2026 23:598,99722,921,7363,131,0002,087,0002,506,752
82Mar 11, 2026 23:599,03122,526,3883,143,0002,095,0002,269,184
83Mar 11, 2026 23:599,11722,098,7504,231,0001,057,0002,273,280
84Mar 11, 2026 23:599,15223,900,7304,247,0001,061,0002,273,280
85Mar 11, 2026 23:5510,72821,917,3685,185,0001,037,0002,465,792
86Mar 11, 2026 23:5510,74323,368,3916,231,00002,519,040
87Mar 11, 2026 23:5510,79724,348,5996,262,00002,404,352
88Mar 11, 2026 23:5510,82123,872,5265,230,0001,046,0002,265,088
89Mar 11, 2026 23:5510,86725,521,8765,253,0001,050,0002,523,136
90Mar 11, 2026 23:5510,96626,218,8756,360,00002,519,040
91Mar 11, 2026 23:5510,98825,562,6775,311,0001,062,0002,506,752
92Mar 11, 2026 23:5511,02232,050,6235,328,0001,065,0002,453,504
93Mar 11, 2026 23:5511,03328,237,0525,333,0001,066,0002,269,184