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 listApr 27, 2026 15:43Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 5, 2026 02:58Tomislav Tunkovic10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 5, 2026 04:26010,886,353940,000020,480No answer to stdout. Died or killed?
2Apr 27, 2026 15:4437,00036,775,34620,439,0001,021,0002,428,928
3Apr 27, 2026 15:4437,79337,672,35419,928,0001,992,0002,428,928
4Apr 27, 2026 15:4438,06240,916,15619,066,0003,010,0002,297,856
5Apr 27, 2026 15:4438,95044,342,74721,565,0001,026,0002,408,448
6Apr 27, 2026 15:4438,99340,803,46421,588,0001,028,0002,478,080
7Apr 27, 2026 15:4439,21841,976,93820,769,0001,978,0002,326,528
8Apr 27, 2026 15:4439,54846,079,37120,944,0001,994,0002,367,488
9Apr 27, 2026 15:4439,56239,686,47420,951,0001,995,0002,527,232
10Apr 27, 2026 15:4439,63144,972,32020,988,0001,998,0002,367,488