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 listDec 27, 2024 10:13Louis PonetLouis PonetError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:13Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:19030,030,7935,228,00014,297,00024,576Exit with code 101: thread 'main' panicked at main.rs:544:21: index out of bounds: the len is 4 but the index is 4 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Dec 27, 2024 10:13287,917190,716,342151,993,00014,999,0002,285,568
3Dec 27, 2024 10:13287,933192,741,702153,001,00014,000,0002,281,472
4Dec 27, 2024 10:13288,255193,016,296157,177,00010,011,0002,367,488
5Dec 27, 2024 10:13288,886194,539,043156,518,00011,036,0002,338,816
6Dec 27, 2024 10:13289,779195,055,810156,067,00012,005,0002,314,240
7Dec 27, 2024 10:13289,874196,436,832157,119,00011,008,0002,367,488
8Dec 27, 2024 10:13289,950196,566,402155,158,00013,013,0002,433,024
9Dec 27, 2024 10:13290,216199,696,129155,300,00013,025,0002,281,472
10Dec 27, 2024 10:13290,284195,575,720154,335,00014,030,0002,265,088