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 GarmierScore: 8,138Success
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, 2026 13:558,00220,158,4773,481,0001,160,0002,289,664
2Mar 26, 2026 15:108,02822,612,2643,492,0001,164,0002,269,184
3Mar 26, 2026 15:108,02823,893,9293,492,0001,164,0002,289,664
4Mar 26, 2026 15:108,03419,932,2663,495,0001,165,0002,367,488
5Mar 26, 2026 15:108,04320,039,0233,499,0001,166,0002,514,944
6Mar 26, 2026 15:108,05523,060,3414,672,00002,277,376
7Mar 26, 2026 13:558,05926,802,7333,506,0001,168,0002,289,664
8Mar 26, 2026 13:558,06218,630,9053,507,0001,169,0002,514,944
9Mar 26, 2026 15:108,06922,097,5193,510,0001,170,0002,514,944
10Mar 26, 2026 13:558,09026,628,4043,519,0001,173,0002,523,136
11Mar 26, 2026 15:448,09826,389,7713,523,0001,174,0002,514,944
12Mar 26, 2026 15:108,12822,267,6743,536,0001,178,0002,514,944
13Mar 26, 2026 13:558,13121,985,9763,537,0001,179,0002,527,232
14Mar 26, 2026 13:558,13822,309,0223,540,0001,180,0002,523,136
15Mar 26, 2026 13:558,14025,166,1443,541,0001,180,0002,289,664
16Mar 26, 2026 15:218,14119,313,7923,542,0001,180,0002,289,664
17Mar 26, 2026 15:108,14319,951,2054,723,00002,527,232
18Mar 26, 2026 15:138,18623,140,2163,561,0001,187,0002,523,136
19Mar 26, 2026 15:218,19325,231,1033,564,0001,188,0002,523,136
20Mar 26, 2026 13:558,19731,633,8383,566,0001,188,0002,289,664
21Mar 26, 2026 15:108,21424,577,6474,764,00002,531,328
22Mar 26, 2026 15:138,22921,461,0643,580,0001,193,0002,514,944
23Mar 26, 2026 15:218,23122,905,5783,581,0001,193,0002,289,664
24Mar 26, 2026 13:558,33423,437,9373,626,0001,208,0002,514,944
25Mar 26, 2026 15:448,46923,056,8252,947,0001,965,0002,277,376
26Mar 26, 2026 15:448,47425,555,6412,949,0001,966,0002,289,664
27Mar 26, 2026 15:138,51921,538,4072,965,0001,976,0002,351,104