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 2, 2026 05:07Zhou180Zhou180Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:03Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:040875,648,547857,878,0007,591,00024,576Exit with code 101: thread 'main' (1) panicked at main.rs:85:24: removal index (is 5) should be < len (is 5) note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Feb 2, 2026 05:07126,80295,082,72768,508,0005,037,0002,355,200
3Feb 2, 2026 05:07127,42199,618,01868,911,0004,993,0002,334,720
4Feb 2, 2026 05:07127,607103,618,81670,012,0004,000,0002,428,928
5Feb 2, 2026 05:07128,048101,238,21569,250,0005,018,0002,318,336
6Feb 2, 2026 05:07128,11698,505,34069,287,0005,020,0002,314,240
7Feb 2, 2026 05:07128,21099,000,44170,343,0004,019,0002,371,584
8Feb 2, 2026 05:07128,319100,257,66469,397,0005,028,0002,441,216
9Feb 2, 2026 05:07128,655102,093,27569,579,0005,041,0002,379,776
10Feb 2, 2026 05:07129,071101,456,72569,871,0004,990,0002,482,176