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 listFeb 7, 2026 15:43lipapipalipapipaError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 16:32Victor Mercklé137.34CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 16:33010,763,8191,711,00002,117,632expected "999999000 ", got "4096000 "
2Jul 5, 2026 16:33010,687,3581,608,00002,117,632expected "999999000 ", got "4096000 "
3Jul 5, 2026 16:33010,854,2781,617,00002,117,632expected "999999000 ", got "4096000 "
4Feb 7, 2026 15:434,60722,502,0252,672,00002,322,432
5Feb 7, 2026 15:434,65918,497,6551,351,0001,351,0002,326,528
6Feb 7, 2026 15:434,73418,049,9811,373,0001,373,0002,314,240
7Feb 7, 2026 15:434,74120,988,9751,375,0001,375,0002,322,432
8Feb 7, 2026 15:434,74524,623,9191,376,0001,376,0002,277,376
9Feb 7, 2026 15:434,80024,379,3931,392,0001,392,0002,514,944
10Feb 7, 2026 15:434,81721,358,1592,794,00002,318,336
11Feb 7, 2026 15:434,86923,039,4332,824,00002,314,240
12Feb 7, 2026 15:434,97620,646,436962,0001,924,0002,293,760