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 10:37Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 4, 2026 12:08Tomislav Tunkovic134.92CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 4, 2026 12:08011,310,7941,529,000020,480No answer to stdout. Died or killed?
2May 5, 2026 10:385,87720,266,4201,885,0001,524,0002,416,640
3May 5, 2026 10:385,88620,671,2993,414,00002,392,064
4May 5, 2026 10:385,89119,930,4233,417,00002,392,064
5May 5, 2026 10:386,05118,221,3941,769,0001,741,0002,482,176
6May 5, 2026 10:386,05620,118,2721,766,0001,747,0002,297,856
7May 5, 2026 10:386,06019,806,9261,765,0001,750,0002,297,856
8May 5, 2026 10:386,07221,991,6551,775,0001,747,0002,514,944
9May 5, 2026 10:386,12718,195,9363,554,00002,342,912
10May 5, 2026 10:386,13118,351,5782,361,0001,195,0002,510,848