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 listMay 5, 2026 11:57Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 5, 2026 01:45Tomislav Tunkovic10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 5, 2026 03:30011,327,3261,561,000020,480No answer to stdout. Died or killed?
2May 5, 2026 11:584,64122,040,5682,692,00002,314,240
3May 5, 2026 11:584,65118,246,1662,698,00002,310,144
4May 5, 2026 11:584,82221,654,4092,797,00002,318,336
5May 5, 2026 11:584,86720,759,4092,823,00002,502,656
6May 5, 2026 11:584,95019,487,9892,871,00002,539,520
7May 5, 2026 11:585,00519,026,6312,903,00002,539,520
8May 5, 2026 11:585,07920,008,1192,946,00002,297,856
9May 5, 2026 11:585,08921,373,9222,952,00002,498,560
10May 5, 2026 11:585,34120,882,5131,859,0001,239,0002,551,808