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:55KeefeKeefeError
Source Code

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

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