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 listDec 18, 2025 15:57Wookyung JeongWookyung JeongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:41Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:41017,420,0277,852,000020,480No answer to stdout. Died or killed?
2Dec 18, 2025 16:0336,02147,380,62219,898,000994,0002,404,352
3Dec 18, 2025 16:0336,42442,593,68019,114,0002,012,0002,420,736
4Dec 18, 2025 15:5736,42647,987,53119,115,0002,012,0002,269,184
5Dec 18, 2025 15:5736,51442,595,79919,161,0002,017,0002,412,544
6Dec 18, 2025 16:0236,57947,833,92519,196,0002,020,0002,510,848
7Dec 18, 2025 15:5736,69143,234,76220,268,0001,013,0002,469,888
8Dec 18, 2025 15:5736,90343,528,83919,366,0002,038,0002,416,640
9Dec 18, 2025 16:0336,95042,041,77821,431,00002,494,464
10Dec 18, 2025 16:0337,01442,936,77821,468,00002,260,992
11Dec 18, 2025 16:0337,06247,503,70420,473,0001,023,0002,482,176
12Dec 18, 2025 15:5737,06945,818,10921,500,00002,412,544
13Dec 18, 2025 16:0337,09341,118,65320,490,0001,024,0002,510,848
14Dec 18, 2025 15:5737,13645,072,77221,539,00002,392,064
15Dec 18, 2025 15:5737,17841,855,81820,537,0001,026,0002,478,080
16Dec 18, 2025 15:5737,30238,824,37920,605,0001,030,0002,387,968
17Dec 18, 2025 15:5737,31242,631,93120,611,0001,030,0002,482,176
18Dec 18, 2025 15:5737,31447,962,37721,642,00002,363,392
19Dec 18, 2025 16:0237,40752,911,07221,696,00002,392,064
20Dec 18, 2025 15:5737,42153,005,73420,671,0001,033,0002,490,368
21Dec 18, 2025 16:0337,46649,693,64620,696,0001,034,0002,445,312
22Dec 18, 2025 16:0337,47448,781,42320,700,0001,035,0002,383,872
23Dec 18, 2025 15:5737,49352,090,39020,711,0001,035,0002,338,816
24Dec 18, 2025 15:5737,52849,025,23719,788,0001,978,0002,523,136
25Dec 18, 2025 16:0237,71049,461,31419,884,0001,988,0002,482,176
26Dec 18, 2025 15:5737,73450,063,27020,892,000994,0002,269,184
27Dec 18, 2025 15:5737,74352,505,25019,901,0001,990,0002,441,216
28Dec 18, 2025 16:0337,76446,087,52819,912,0001,991,0002,433,024