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 18, 2026 22:22Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 14:31Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 14:3109,489,7811,736,00002,117,632expected "999999000 ", got "0 "
2Jul 5, 2026 14:3109,504,8401,739,00002,117,632expected "999999000 ", got "0 "
3Jul 5, 2026 14:3109,501,9611,745,00002,117,632expected "999999000 ", got "0 "
4Mar 18, 2026 22:243,34519,892,536970,000970,0002,383,872
5Mar 18, 2026 22:243,43419,386,039996,000996,0002,392,064
6Mar 18, 2026 22:243,44117,604,499998,000998,0002,400,256
7Mar 18, 2026 22:243,46918,162,5041,006,0001,006,0002,539,520
8Mar 18, 2026 22:243,50724,185,40102,034,0002,363,392
9Mar 18, 2026 22:243,65520,087,54002,120,0002,445,312
10Mar 18, 2026 22:243,73821,306,20802,168,0002,408,448
11Mar 18, 2026 22:243,74121,002,0431,085,0001,085,0002,392,064
12Mar 18, 2026 22:233,75919,254,9051,090,0001,090,0002,375,680
13Mar 18, 2026 22:243,81719,025,0361,107,0001,107,0002,326,528
14Mar 18, 2026 22:233,90016,956,1702,262,00002,367,488
15Mar 18, 2026 22:224,05517,446,6042,352,00002,400,256
16Mar 18, 2026 22:234,05919,543,2011,177,0001,177,0002,465,792
17Mar 18, 2026 22:234,10023,941,1011,189,0001,189,0002,387,968
18Mar 18, 2026 22:234,20319,488,2691,219,0001,219,0002,342,912
19Mar 18, 2026 22:224,21217,562,3402,443,00002,326,528
20Mar 18, 2026 22:224,49716,413,4921,304,0001,304,0002,383,872
21Mar 18, 2026 22:234,53423,500,3691,315,0001,315,0002,383,872
22Mar 18, 2026 22:225,78822,684,0582,238,0001,119,0002,437,120
23Mar 18, 2026 22:225,80018,198,9313,364,00002,330,624
24Mar 18, 2026 22:226,01018,778,0903,486,00002,531,328