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 05:21RussellStrongRussellStrongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:29Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:39011,639,365944,000028,672Exit with code 101: thread 'main' (1) panicked at main.rs:69:54: 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 05:21346,090230,432,654197,736,0002,996,0002,289,664
3Dec 22, 2025 05:27347,148225,695,496196,338,0005,008,0002,289,664
4Dec 22, 2025 05:21348,286227,683,891200,006,0002,000,0002,289,664
5Dec 22, 2025 05:27349,126224,248,108199,486,0003,007,0002,355,200
6Dec 22, 2025 05:21349,721228,279,310197,842,0004,996,0002,347,008
7Dec 22, 2025 05:21350,009228,216,656202,005,0001,000,0002,347,008
8Dec 22, 2025 05:21350,040227,607,549198,023,0005,000,0002,289,664
9Dec 22, 2025 05:21351,040226,400,887201,598,0002,005,0002,289,664
10Dec 22, 2025 05:21351,681228,325,250201,976,0001,999,0002,351,104
11Dec 22, 2025 05:27353,141230,877,496204,822,00002,465,792
12Dec 22, 2025 05:21368,200237,107,118209,546,0004,010,0002,285,568
13Dec 22, 2025 05:21368,279243,184,155213,602,00002,289,664