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 listOct 17, 2026 07:42janwwwjanwwwError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 29, 2026 22:12Andrew Epstein19.25CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 29, 2026 22:130226,050,708189,862,00022,905,0002,117,632expected "872762873 ", got "943000030 "
2Oct 29, 2026 22:130225,002,336190,888,00023,196,0002,117,632expected "864817330 ", got "943000030 "
3Oct 29, 2026 22:130224,545,600189,709,00024,007,0002,117,632expected "871776758 ", got "966000007 "
4Oct 17, 2026 07:4233,67930,681,97118,463,0001,071,0002,117,632
5Oct 17, 2026 07:4233,85332,128,47718,560,0001,075,0002,117,632
6Oct 17, 2026 07:4234,39130,684,23818,857,0001,090,0002,117,632
7Oct 17, 2026 07:4234,46530,723,61618,902,0001,088,0002,117,632
8Oct 17, 2026 07:4234,52930,690,68518,934,0001,093,0002,117,632
9Oct 17, 2026 07:4235,05631,532,70419,228,0001,105,0002,117,632
10Oct 17, 2026 07:4235,17231,139,17319,296,0001,104,0002,117,632
11Oct 17, 2026 07:4235,27031,271,65619,292,0001,165,0002,117,632
12Oct 17, 2026 07:4235,38431,217,74919,394,0001,129,0002,117,632