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 24, 2026 12:33Jem BishopJem BishopError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 14:33Victor Mercklé12.48CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 14:33010,699,9361,439,000024,576No answer to stdout. Died or killed?
2Feb 24, 2026 12:333,75917,730,6071,090,0001,090,0002,293,760
3Feb 24, 2026 12:333,84117,783,95802,228,0002,334,720
4Feb 24, 2026 12:333,84519,208,7331,115,0001,115,0002,330,624
5Feb 24, 2026 12:333,84821,739,9551,116,0001,116,0002,424,832
6Feb 24, 2026 12:333,85223,964,03702,234,0002,342,912
7Feb 24, 2026 12:333,86919,213,75402,244,0002,338,816
8Feb 24, 2026 12:333,88620,249,2751,127,0001,127,0002,338,816
9Feb 24, 2026 12:333,90721,636,6721,133,0001,133,0002,338,816
10Feb 24, 2026 12:333,91719,776,6102,272,00002,334,720
11Feb 24, 2026 12:333,92422,471,5651,138,0001,138,0002,338,816
12Feb 24, 2026 12:334,01419,900,5971,164,0001,164,0002,334,720
13Feb 24, 2026 12:334,07425,125,6032,363,00002,342,912
14Feb 24, 2026 12:334,08620,164,5532,370,00002,330,624
15Feb 24, 2026 12:334,13318,292,6212,397,00002,289,664
16Feb 24, 2026 12:334,17920,993,5051,212,0001,212,0002,289,664
17Feb 24, 2026 12:334,19819,936,6812,435,00002,338,816
18Feb 24, 2026 12:334,56619,496,4031,324,0001,324,0002,334,720
19Feb 24, 2026 12:334,63420,154,0191,344,0001,344,0002,330,624