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 listAug 16, 2026 23:12Tomislav TunkovicTomislav TunkovicError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:56Andrew Epstein10.00CustomSolution Broken
Sep 4, 2026 13:50Victor Mercklé10.00CustomSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:58012,490,484999,000028,672Exit with code 101: thread 'main' (1) panicked at main.rs:531:15: removal index (is 1) should be < len (is 0) note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Aug 17, 2026 05:0225,33124,500,38813,605,0001,087,00024,576
3Aug 17, 2026 05:0225,56524,696,03713,808,0001,020,00024,576
4Aug 17, 2026 05:0225,61324,597,38813,803,0001,053,00024,576
5Aug 17, 2026 05:0225,69124,712,72313,868,0001,033,00024,576
6Aug 17, 2026 05:0225,87224,845,44913,957,0001,049,00024,576
7Aug 17, 2026 05:0226,13925,009,83614,104,0001,057,00024,576
8Aug 17, 2026 05:0226,17725,009,64114,110,0001,073,00024,576
9Aug 17, 2026 05:0226,26725,754,33014,188,0001,047,00024,576
10Aug 17, 2026 05:0226,52225,152,93414,176,0001,207,00024,576