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:44Dominique GarmierDominique GarmierError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 18:43Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 18:43011,191,92301,850,0002,121,728expected "999999000 ", got "16959000 "
2Jul 5, 2026 18:43011,166,49301,849,0002,121,728expected "999999000 ", got "16959000 "
3Jul 5, 2026 18:43011,636,85901,992,0002,121,728expected "999999000 ", got "16959000 "
4Mar 26, 2026 13:558,00220,158,4773,481,0001,160,0002,289,664
5Mar 26, 2026 15:108,02822,612,2643,492,0001,164,0002,269,184
6Mar 26, 2026 15:108,02823,893,9293,492,0001,164,0002,289,664
7Mar 26, 2026 15:108,03419,932,2663,495,0001,165,0002,367,488
8Mar 26, 2026 15:108,04320,039,0233,499,0001,166,0002,514,944
9Mar 26, 2026 15:108,05523,060,3414,672,00002,277,376
10Mar 26, 2026 13:558,05926,802,7333,506,0001,168,0002,289,664
11Mar 26, 2026 13:558,06218,630,9053,507,0001,169,0002,514,944
12Mar 26, 2026 15:108,06922,097,5193,510,0001,170,0002,514,944
13Mar 26, 2026 13:558,09026,628,4043,519,0001,173,0002,523,136
14Mar 26, 2026 15:448,09826,389,7713,523,0001,174,0002,514,944
15Mar 26, 2026 15:108,12822,267,6743,536,0001,178,0002,514,944
16Mar 26, 2026 13:558,13121,985,9763,537,0001,179,0002,527,232
17Mar 26, 2026 13:558,13822,309,0223,540,0001,180,0002,523,136
18Mar 26, 2026 13:558,14025,166,1443,541,0001,180,0002,289,664
19Mar 26, 2026 15:218,14119,313,7923,542,0001,180,0002,289,664
20Mar 26, 2026 15:108,14319,951,2054,723,00002,527,232
21Mar 26, 2026 15:138,18623,140,2163,561,0001,187,0002,523,136
22Mar 26, 2026 15:218,19325,231,1033,564,0001,188,0002,523,136
23Mar 26, 2026 13:558,19731,633,8383,566,0001,188,0002,289,664
24Mar 26, 2026 15:108,21424,577,6474,764,00002,531,328
25Mar 26, 2026 15:138,22921,461,0643,580,0001,193,0002,514,944
26Mar 26, 2026 15:218,23122,905,5783,581,0001,193,0002,289,664
27Mar 26, 2026 13:558,33423,437,9373,626,0001,208,0002,514,944
28Mar 26, 2026 15:448,46923,056,8252,947,0001,965,0002,277,376
29Mar 26, 2026 15:448,47425,555,6412,949,0001,966,0002,289,664
30Mar 26, 2026 15:138,51921,538,4072,965,0001,976,0002,351,104