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 listJul 15, 2025 13:37nolipolsupnolipolsupError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:38Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:420935,699,270924,241,0001,000,00024,576Exit with code 101: thread 'main' panicked at main.rs:98:50: called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit } note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Jul 15, 2025 13:37430,936275,898,194245,944,0003,999,0002,289,664
3Jul 15, 2025 13:37431,159275,118,190248,072,0002,000,0002,334,720
4Jul 15, 2025 13:37431,291273,313,464249,149,0001,000,0002,342,912
5Jul 15, 2025 13:37431,329274,558,272247,169,0003,002,0002,330,624
6Jul 15, 2025 13:37431,612272,956,123249,334,0001,001,0002,433,024
7Jul 15, 2025 13:37432,388277,178,618245,790,0004,995,0002,437,120
8Jul 15, 2025 13:37432,414279,279,103248,802,0001,998,0002,449,408
9Jul 15, 2025 13:37438,471279,176,770254,313,00002,277,376
10Jul 15, 2025 13:37440,741279,521,635253,626,0002,004,0002,342,912