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 6, 2026 23:43Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 17:06Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 17:06011,369,0351,935,000020,480No answer to stdout. Died or killed?
2Dec 28, 2025 18:135,51918,149,0061,067,0002,134,0002,322,432
3Feb 6, 2026 23:435,59821,728,0892,165,0001,082,0002,314,240
4Dec 28, 2025 18:135,66423,163,8832,190,0001,095,0002,265,088
5Feb 6, 2026 23:435,68120,621,7722,197,0001,098,0002,277,376
6Dec 28, 2025 18:135,73119,302,4682,216,0001,108,0002,490,368
7Dec 28, 2025 18:135,73320,256,1342,217,0001,108,0002,273,280
8Dec 28, 2025 18:135,77120,812,8883,347,00002,412,544
9Dec 28, 2025 18:135,82423,753,9503,378,00002,379,776
10Feb 6, 2026 23:435,85023,344,1682,262,0001,131,0002,273,280
11Dec 28, 2025 18:135,85220,602,1473,394,00002,478,080
12Feb 6, 2026 23:435,85220,667,0082,263,0001,131,0002,347,008
13Dec 28, 2025 18:135,92626,375,8873,437,00002,269,184
14Feb 6, 2026 23:435,93824,100,7982,296,0001,148,0002,277,376
15Feb 6, 2026 23:435,94018,914,9882,297,0001,148,0002,318,336
16Dec 28, 2025 18:135,95024,293,1623,451,00002,457,600