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 29, 2025 21:54Aniruddha DebAniruddha DebError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:23Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:23011,334,0491,896,00002,117,632expected "999999000 ", got "0 "
2Jul 5, 2026 20:2309,691,5851,920,00002,117,632expected "999999000 ", got "0 "
3Jul 5, 2026 20:2309,569,6891,895,00002,117,632expected "999999000 ", got "0 "
4Dec 29, 2025 21:5412,18326,882,7156,057,0001,009,0002,527,232
5Dec 29, 2025 21:5412,64125,432,9757,332,00002,269,184
6Dec 29, 2025 21:5413,13324,755,9907,617,00002,469,888
7Dec 29, 2025 21:5413,64524,500,3856,925,000989,0002,469,888
8Dec 29, 2025 21:5414,52126,739,9767,370,0001,052,0002,453,504
9Dec 29, 2025 21:5415,60223,008,6629,049,00002,469,888
10Dec 29, 2025 21:5416,74026,727,0339,709,00002,510,848
11Dec 29, 2025 21:5418,78326,031,6868,914,0001,980,0002,510,848
12Dec 29, 2025 21:5419,08327,468,12110,062,0001,006,0002,457,600