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 8, 2025 22:18undenuicapundenuicapError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:12Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:15061,011,67950,778,000020,480No answer to stdout. Died or killed?
2Feb 8, 2025 22:18183,584130,084,751105,475,0001,004,0002,285,568
3Feb 8, 2025 22:18183,745135,217,718105,567,0001,005,0002,277,376
4Feb 8, 2025 22:18183,895133,526,617106,659,00002,359,296
5Feb 8, 2025 22:18184,291132,156,090104,892,0001,997,0002,355,200
6Feb 8, 2025 22:18184,350131,518,834105,924,000999,0002,359,296
7Feb 8, 2025 22:18184,543132,517,296106,035,0001,000,0002,367,488
8Feb 8, 2025 22:18184,712135,582,847106,132,0001,001,0002,285,568
9Feb 8, 2025 22:18184,757134,110,185106,158,0001,001,0002,363,392
10Feb 8, 2025 22:18185,524136,048,504105,612,0001,992,0002,285,568