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 listDec 24, 2025 02:58RussellStrongRussellStrongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:40Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:52010,962,075934,000028,672Exit with code 101: thread 'main' (1) panicked at main.rs:132:34: explicit panic note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Jan 2, 2026 05:40110,48886,427,39461,080,0003,003,0002,289,664
3Jan 2, 2026 05:40110,83486,026,80863,280,0001,004,0002,363,392
4Dec 24, 2025 02:58110,91990,105,96863,328,0001,005,0002,289,664
5Jan 2, 2026 05:41111,16786,030,11562,463,0002,014,0002,289,664
6Jan 2, 2026 05:41111,21085,383,80762,487,0002,015,0002,359,296
7Dec 24, 2025 02:58111,29185,148,81263,541,0001,008,0002,289,664
8Dec 24, 2025 02:58111,86089,607,00964,879,00002,359,296
9Dec 24, 2025 02:58111,90792,884,86862,909,0001,997,0002,363,392
10Dec 24, 2025 02:58112,05590,520,65462,993,0001,999,0002,289,664
11Dec 24, 2025 02:58112,066100,484,92661,999,0002,999,0002,289,664
12Jan 2, 2026 05:41112,30291,543,88964,133,0001,002,0002,342,912
13Dec 24, 2025 02:58112,47692,798,00862,226,0003,010,0002,355,200
14Jan 2, 2026 05:40112,64787,449,75464,330,0001,005,0002,347,008
15Dec 24, 2025 02:58112,70391,087,63463,357,0002,011,0002,482,176
16Dec 24, 2025 02:58115,29393,258,50064,874,0001,996,0002,359,296