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 28, 2025 19:05yenw0dyenw0dError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:09Victor Mercklé21.32CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:09013,789,77504,291,00024,576No answer to stdout. Died or killed?
2Dec 28, 2025 19:0511,92126,142,1932,963,0003,951,0002,359,296
3Dec 28, 2025 19:0412,01731,087,4791,991,0004,979,0002,301,952
4Dec 28, 2025 19:0412,10023,533,3711,002,0006,016,0002,428,928
5Dec 28, 2025 19:0412,17426,389,8503,026,0004,035,0002,379,776
6Dec 28, 2025 19:0412,23424,089,9423,041,0004,055,0002,387,968
7Dec 28, 2025 19:0412,25228,287,0151,015,0006,091,0002,301,952
8Dec 28, 2025 19:0412,31423,275,2991,020,0006,122,0002,490,368
9Dec 28, 2025 19:0412,37227,100,1451,025,0006,151,0002,482,176
10Dec 28, 2025 19:0512,48826,160,2031,034,0006,209,0002,342,912
11Dec 28, 2025 19:0412,59028,177,9411,043,0006,259,0002,326,528
12Dec 28, 2025 19:0412,68628,052,9581,051,0006,307,0002,363,392
13Dec 28, 2025 19:0513,30737,816,9622,205,0005,513,0002,523,136