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 15:55yenw0dyenw0dError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:29Victor Mercklé11.16CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:29014,349,2441,150,0003,757,00024,576No answer to stdout. Died or killed?
2Dec 28, 2025 15:5515,67228,138,5864,040,0005,050,0002,277,376
3Dec 28, 2025 15:5515,82825,303,3405,100,0004,080,0002,396,160
4Dec 28, 2025 15:5515,90031,824,8965,123,0004,099,0002,396,160
5Dec 28, 2025 15:5516,10726,598,2295,190,0004,152,0002,510,848
6Dec 28, 2025 15:5516,69831,079,6263,228,0006,457,0002,322,432
7Dec 28, 2025 15:5516,75525,895,8675,399,0004,319,0002,367,488
8Dec 28, 2025 15:5517,10925,656,6163,969,0005,954,0002,289,664
9Dec 28, 2025 15:5517,25929,540,1364,004,0006,006,0002,482,176
10Dec 28, 2025 15:5517,32431,830,6985,024,0005,024,0002,498,560