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 12, 2026 19:36pseppsepError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:29Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:330173,277,720152,933,0005,615,0002,113,536expected "873699857 ", got "0 "
2Oct 5, 2026 18:330167,915,166152,046,0005,582,0002,113,536expected "870748126 ", got "0 "
3Oct 5, 2026 18:330168,748,486152,838,0005,614,0002,113,536expected "875668405 ", got "42651 "
4Mar 12, 2026 19:36373,050243,087,871206,352,00010,017,0002,269,184
5Mar 12, 2026 19:36373,679245,404,537208,744,0007,990,0002,400,256
6Mar 12, 2026 19:36373,691242,029,405208,751,0007,990,0002,412,544
7Mar 12, 2026 19:36373,821240,588,798207,824,0008,992,0002,273,280
8Mar 12, 2026 19:36373,847241,687,143208,838,0007,993,0002,371,584
9Mar 12, 2026 19:36374,100242,530,257209,979,0006,999,0002,318,336
10Mar 12, 2026 19:36374,540244,314,540208,224,0009,009,0002,273,280
11Mar 12, 2026 19:36375,405243,738,822208,746,0008,989,0002,379,776
12Mar 12, 2026 19:36375,440242,656,267206,768,00010,987,0002,342,912