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 16, 2025 20:40Bhavik-MakwanaBhavik-MakwanaError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:00Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:07013,422,9121,046,0002,111,00028,672Exit with code 101: thread 'main' panicked at main.rs:58:21: index out of bounds: the len is 6000 but the index is 21994 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Feb 16, 2025 20:40103,96983,536,35556,282,0004,020,0002,408,448
3Feb 16, 2025 20:40104,14381,353,09858,390,0002,013,0002,408,448
4Feb 16, 2025 20:40104,94584,863,59457,875,0002,993,0002,285,568
5Feb 16, 2025 20:40105,68884,949,44759,290,0002,009,0002,277,376
6Feb 16, 2025 20:40108,57189,682,52958,973,0003,998,0002,289,664
7Feb 16, 2025 20:40110,52192,891,89560,096,0004,006,0002,285,568
8Feb 16, 2025 20:40110,69891,682,69561,196,0003,009,0002,289,664
9Feb 16, 2025 20:40112,55591,152,65562,269,0003,013,0002,404,352
10Feb 16, 2025 20:40115,83393,219,43664,175,0003,008,0002,289,664
11Feb 16, 2025 20:40137,743104,252,92576,896,0002,995,0002,285,568
12Feb 16, 2025 20:40141,198108,418,08879,898,0001,997,0002,408,448
13Feb 16, 2025 20:40159,595121,700,99691,559,0001,006,0002,408,448