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, 2026 15:31Dominique GarmierDominique GarmierError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 18:42Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 18:42011,340,2022,039,000020,480No answer to stdout. Died or killed?
2Mar 26, 2026 15:287,61421,634,4293,312,0001,104,0002,314,240
3Mar 26, 2026 15:337,62125,194,7433,315,0001,105,0002,334,720
4Mar 26, 2026 15:347,63422,254,0783,321,0001,107,0002,486,272
5Mar 26, 2026 15:347,63624,372,1824,429,00002,285,568
6Mar 26, 2026 15:337,66021,205,4494,443,00002,318,336
7Mar 26, 2026 15:337,69821,536,7253,349,0001,116,0002,318,336
8Mar 26, 2026 15:287,71721,164,1012,238,0002,238,0002,482,176
9Mar 26, 2026 15:337,71925,213,2893,358,0001,119,0002,482,176
10Mar 26, 2026 15:287,74821,370,2124,494,00002,318,336
11Mar 26, 2026 15:357,75223,606,9483,372,0001,124,0002,326,528
12Mar 26, 2026 15:307,76621,117,8193,378,0001,126,0002,322,432
13Mar 26, 2026 15:347,76920,275,2114,506,00002,281,472
14Mar 26, 2026 15:337,81024,322,5683,398,0001,132,0002,482,176
15Mar 26, 2026 15:317,81020,487,6212,265,0002,265,0002,314,240
16Mar 26, 2026 15:297,81423,044,0563,399,0001,133,0002,486,272
17Mar 26, 2026 15:287,82122,228,5952,268,0002,268,0002,330,624
18Mar 26, 2026 15:287,82821,088,2953,405,0001,135,0002,318,336
19Mar 26, 2026 15:337,86221,521,0693,420,0001,140,0002,318,336
20Mar 26, 2026 15:317,86223,525,2864,560,00002,318,336
21Mar 26, 2026 15:297,86423,601,7873,421,0001,140,0002,424,832
22Mar 26, 2026 15:287,88327,464,7523,429,0001,143,0002,326,528
23Mar 26, 2026 15:287,89119,235,9943,433,0001,144,0002,273,280
24Mar 26, 2026 15:337,89319,453,5312,289,0002,289,0002,318,336
25Mar 26, 2026 15:287,89821,103,0253,436,0001,145,0002,277,376
26Mar 26, 2026 15:297,90025,789,4033,437,0001,145,0002,277,376
27Mar 26, 2026 15:357,90325,182,0612,292,0002,292,0002,314,240
28Mar 26, 2026 15:337,92123,707,8182,297,0002,297,0002,322,432
29Mar 26, 2026 15:337,93326,114,8223,451,0001,150,0002,318,336
30Mar 26, 2026 15:357,93820,431,9733,453,0001,151,0002,428,928
31Mar 26, 2026 15:307,95223,178,5633,459,0001,153,0002,326,528
32Mar 26, 2026 15:357,95321,751,2273,460,0001,153,0002,269,184
33Mar 26, 2026 15:317,95527,480,5194,614,00002,273,280
34Mar 26, 2026 15:357,96621,288,4753,465,0001,155,0002,482,176
35Mar 26, 2026 15:297,96922,262,7274,622,00002,342,912
36Mar 26, 2026 15:287,97625,006,5123,470,0001,156,0002,318,336
37Mar 26, 2026 15:287,99021,248,7213,476,0001,158,0002,322,432
38Mar 26, 2026 15:317,99322,001,2602,318,0002,318,0002,318,336
39Mar 26, 2026 15:357,99721,475,1033,479,0001,159,0002,322,432
40Mar 26, 2026 15:358,00730,627,3773,483,0001,161,0002,326,528
41Mar 26, 2026 15:348,01629,158,5633,487,0001,162,0002,322,432
42Mar 26, 2026 15:298,02821,360,5803,492,0001,164,0002,433,024
43Mar 26, 2026 15:288,02921,818,2133,493,0001,164,0002,273,280
44Mar 26, 2026 15:348,04821,327,3164,668,00002,322,432
45Mar 26, 2026 15:378,05921,275,6463,506,0001,168,0002,322,432
46Mar 26, 2026 15:378,06726,021,6454,679,00002,342,912
47Mar 26, 2026 15:338,07221,357,4664,682,00002,433,024
48Mar 26, 2026 15:338,07821,637,9593,514,0001,171,0002,322,432
49Mar 26, 2026 15:358,10322,196,7423,525,0001,175,0002,326,528
50Mar 26, 2026 15:288,10723,202,1123,527,0001,175,0002,322,432
51Mar 26, 2026 15:318,11222,224,3284,705,00002,273,280
52Mar 26, 2026 15:318,14820,531,4393,545,0001,181,0002,330,624
53Mar 26, 2026 15:288,16224,108,5894,734,00002,478,080
54Mar 26, 2026 15:288,17220,527,8553,555,0001,185,0002,342,912
55Mar 26, 2026 15:318,17424,934,7123,556,0001,185,0002,322,432
56Mar 26, 2026 15:308,18327,192,6213,560,0001,186,0002,330,624
57Mar 26, 2026 15:348,19022,109,6443,563,0001,187,0002,273,280
58Mar 26, 2026 15:378,21921,932,7394,767,00002,269,184
59Mar 26, 2026 15:288,22121,614,7632,384,0002,384,0002,314,240
60Mar 26, 2026 15:298,27625,054,2763,600,0001,200,0002,326,528
61Mar 26, 2026 15:358,30322,508,9613,612,0001,204,0002,269,184
62Mar 26, 2026 15:318,30723,825,9693,614,0001,204,0002,326,528
63Mar 26, 2026 15:338,51725,154,0952,964,0001,976,0002,478,080
64Mar 26, 2026 15:318,67822,358,0343,020,0002,013,0002,318,336