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 LyfenkoScore: 3,900Success
Source Code

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

Challenge History

No challenges yet.

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