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 listJan 14, 2025 22:37korey-skorey-sError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:13Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:16011,216,7111,024,000028,672Exit with code 101: thread 'main' panicked at main.rs:84:41: Out of bounds access note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Jan 14, 2025 22:3788,60274,726,47050,382,0001,007,0002,482,176
3Jan 14, 2025 22:3788,90275,968,37251,563,00002,510,848
4Jan 14, 2025 22:3789,04585,423,53851,646,00002,482,176
5Jan 14, 2025 22:3789,22176,615,44450,734,0001,014,0002,306,048
6Jan 14, 2025 22:3789,40581,108,41050,858,000997,0002,506,752
7Jan 14, 2025 22:3789,45274,971,14550,885,000997,0002,404,352
8Jan 14, 2025 22:3789,54377,484,53249,938,0001,997,0002,301,952
9Jan 14, 2025 22:3789,59385,719,24450,965,000999,0002,506,752
10Jan 14, 2025 22:3791,59376,172,62851,120,0002,004,0002,412,544
11Jan 14, 2025 22:3798,68182,722,04955,227,0002,008,0002,285,568
12Jan 14, 2025 22:37101,69788,421,34756,985,0001,999,0002,289,664
13Jan 14, 2025 22:37102,82186,255,89158,626,0001,010,0002,281,472