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 22, 2025 04:48RussellStrongRussellStrongError
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,992,286996,000028,672Exit with code 101: thread 'main' (1) panicked at main.rs:70:57: called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit } note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Dec 22, 2025 04:48449,378290,018,275258,635,0002,004,0002,347,008
3Dec 22, 2025 04:48450,209288,202,672261,121,00002,289,664
4Dec 22, 2025 04:48450,243284,840,176261,141,00002,289,664
5Dec 22, 2025 04:51450,609284,172,374258,349,0003,004,0002,289,664
6Dec 22, 2025 04:51452,412285,017,511262,399,00002,289,664
7Dec 22, 2025 04:51453,095289,979,774260,797,0001,998,0002,289,664
8Dec 22, 2025 04:51453,498285,124,579260,029,0003,000,0002,334,720
9Dec 22, 2025 04:51453,848289,532,883262,232,0001,000,0002,289,664
10Dec 22, 2025 04:48453,876288,296,116261,247,0002,001,0002,289,664
11Dec 22, 2025 04:48455,119285,019,524259,970,0003,999,0002,478,080
12Dec 22, 2025 04:48455,369288,190,765260,113,0004,001,0002,347,008
13Dec 22, 2025 04:48455,541285,367,561262,213,0002,001,0002,289,664
14Dec 22, 2025 04:48457,945294,519,146264,606,0001,002,0002,342,912
15Dec 22, 2025 04:51458,267292,496,572262,798,0002,997,0002,338,816
16Dec 22, 2025 04:51465,140302,382,539266,784,0002,997,0002,289,664
17Dec 22, 2025 04:48468,786299,844,095269,897,0001,999,0002,338,816
18Dec 22, 2025 04:51470,603298,556,325270,951,0001,999,0002,289,664
19Dec 22, 2025 04:51472,521298,476,108271,062,0003,000,0002,285,568