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 29, 2025 02:46Carl BergmanCarl BergmanError
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:32075,530,19854,448,0008,006,0002,117,632expected "875659775 ", got "8015850 "
2Oct 5, 2026 18:32074,976,77056,409,0007,564,0002,121,728expected "883525003 ", got "7510582 "
3Oct 5, 2026 18:32077,772,45058,090,0008,433,0002,117,632expected "873726170 ", got "173000 "
4Dec 29, 2025 02:46146,379112,131,11280,858,0004,042,0002,449,408
5Dec 29, 2025 02:46146,633109,499,94479,044,0006,003,0002,293,760
6Dec 29, 2025 02:46146,903113,343,40480,192,0005,012,0002,285,568
7Dec 29, 2025 02:46147,038112,252,07381,269,0004,013,0002,285,568
8Dec 29, 2025 02:46147,383110,074,80580,454,0005,028,0002,347,008
9Dec 29, 2025 02:46147,448110,592,61980,490,0005,030,0002,502,656
10Dec 29, 2025 02:46147,472111,927,41081,509,0004,025,0002,510,848
11Dec 29, 2025 02:46148,452113,523,11482,098,0004,004,0002,330,624
12Dec 29, 2025 02:46149,321115,336,04882,578,0004,028,0002,502,656