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 21, 2025 11:37parastromparastromError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:59Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:02026,161,36214,542,0001,070,0002,117,632expected "875692392 ", got "262144 "
2Oct 5, 2026 19:02026,000,50414,502,0001,135,0002,117,632expected "873681092 ", got "3786091 "
3Oct 5, 2026 19:02026,030,56514,589,0001,131,0002,117,632expected "869759605 ", got "0 "
4Dec 21, 2025 11:3771,33174,325,03140,363,0001,009,0002,289,664
5Dec 21, 2025 11:3771,77269,505,08840,613,0001,015,0002,351,104
6Dec 21, 2025 11:3772,22269,500,43040,892,000997,0002,355,200
7Dec 21, 2025 11:3773,27269,172,65942,498,00002,289,664
8Dec 21, 2025 11:3773,92673,330,52342,877,00002,289,664
9Dec 21, 2025 11:3773,94069,351,75540,891,0001,994,0002,359,296
10Dec 21, 2025 11:3773,94168,733,06540,892,0001,994,0002,285,568
11Dec 21, 2025 11:3774,90766,192,17543,446,00002,285,568
12Dec 21, 2025 11:3777,50981,481,77542,957,0001,998,0002,285,568