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 listMar 2, 2026 20:31JuneLJuneLError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:02Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:04018,024,61807,960,00028,672Exit with code 101: thread 'main' (1) panicked at main.rs:81:21: index out of bounds: the len is 20001 but the index is 21980 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Mar 2, 2026 20:31109,10591,372,74157,255,0006,026,0002,424,832
3Mar 2, 2026 20:31109,39589,607,08957,407,0006,042,0002,424,832
4Mar 2, 2026 20:31111,94896,366,61858,937,0005,993,0002,457,600
5Mar 2, 2026 20:31111,99199,708,52058,960,0005,995,0002,514,944
6Mar 2, 2026 20:31114,35592,912,80860,297,0006,029,0002,330,624
7Mar 2, 2026 20:31119,53695,277,72863,303,0006,028,0002,363,392
8Mar 2, 2026 20:31120,84397,936,24465,083,0005,006,0002,396,160
9Mar 2, 2026 20:31123,29396,845,73365,467,0006,043,0002,330,624
10Mar 2, 2026 20:31134,467100,969,66271,992,0005,999,0002,461,696