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 listFeb 10, 2025 22:46Daniel HarrisDaniel HarrisError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:26Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:27013,886,5121,235,0002,479,00028,672Exit with code 101: thread 'main' panicked at ring_buffer.rs:21:20: index out of bounds: the len is 5000 but the index is 21695 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Feb 10, 2025 22:4685,99778,765,57047,883,0001,995,0002,289,664
3Feb 10, 2025 22:4690,28378,243,36949,343,0003,021,0002,281,472
4Feb 10, 2025 22:4690,28875,531,00648,339,0004,028,0002,281,472
5Feb 10, 2025 22:4690,52873,369,65149,477,0003,029,0002,289,664
6Feb 10, 2025 22:4693,23479,176,29152,074,0002,002,0002,412,544
7Feb 10, 2025 22:4693,69884,694,15251,326,0003,019,0002,289,664
8Feb 10, 2025 22:46102,08888,052,53756,201,0003,010,0002,289,664
9Feb 10, 2025 22:46102,22984,481,70356,279,0003,014,0002,289,664
10Feb 10, 2025 22:46108,94890,271,19760,181,0003,009,0002,289,664
11Feb 10, 2025 22:46118,738102,701,27865,874,0002,994,0002,289,664
12Feb 10, 2025 22:45126,93695,711,94273,623,00002,383,872
13Feb 10, 2025 22:45127,131105,135,81971,716,0002,020,0002,400,256
14Feb 10, 2025 22:46129,840104,230,26272,295,0003,012,0002,289,664
15Feb 10, 2025 22:45142,869107,694,57580,868,0001,996,0002,269,184
16Feb 10, 2025 22:46149,303116,319,01385,590,0001,006,0002,289,664