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 24, 2026 11:15Jem BishopJem BishopError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:56Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:56017,647,7238,148,000024,576No answer to stdout. Died or killed?
2Feb 24, 2026 11:1536,07650,791,36919,928,000996,0002,334,720
3Feb 24, 2026 11:1536,84142,532,53620,351,0001,017,0002,453,504
4Feb 24, 2026 11:1537,37148,807,11321,675,00002,326,528
5Feb 24, 2026 11:1537,80948,457,82520,933,000996,0002,293,760
6Feb 24, 2026 11:1537,86248,428,29120,915,0001,045,0002,334,720
7Feb 24, 2026 11:1537,87953,423,99619,973,0001,997,0002,293,760
8Feb 24, 2026 11:1538,08647,779,61121,086,0001,004,0002,330,624
9Feb 24, 2026 11:1538,14142,736,35420,111,0002,011,0002,293,760
10Feb 24, 2026 11:1538,45043,552,07620,274,0002,027,0002,330,624