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 7, 2025 12:37Daniel HarrisDaniel HarrisError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:11Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:13011,246,0251,338,000032,768Exit with code 101: thread 'main' panicked at main.rs:73:9: index out of bounds: the len is 8192 but the index is 21984 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Feb 7, 2025 12:4391,18880,386,94951,892,000997,0002,428,928
3Feb 7, 2025 12:3892,53385,872,08252,657,0001,012,0002,412,544
4Feb 7, 2025 12:4393,38677,813,97552,158,0002,006,0002,433,024
5Feb 7, 2025 12:3798,28481,469,76756,005,0001,000,0002,383,872
6Feb 7, 2025 12:38102,03884,731,27257,176,0002,006,0002,428,928
7Feb 7, 2025 12:43102,23480,818,45157,286,0002,010,0002,387,968
8Feb 7, 2025 12:37103,26086,297,10557,895,0001,996,0002,486,272
9Feb 7, 2025 12:38103,40588,259,93758,976,000999,0002,469,888
10Feb 7, 2025 12:37105,87289,308,41861,406,00002,420,736
11Feb 7, 2025 12:37120,93492,327,98269,140,0001,002,0002,478,080
12Feb 7, 2025 12:37122,94195,397,04769,298,0002,008,0002,367,488
13Feb 7, 2025 12:43124,24896,624,16571,064,0001,000,0002,486,272
14Feb 7, 2025 12:37124,44897,999,88170,175,0002,005,0002,383,872
15Feb 7, 2025 12:38124,87292,171,45771,421,0001,005,0002,482,176
16Feb 7, 2025 12:37124,99798,151,24570,485,0002,013,0002,387,968
17Feb 7, 2025 12:37125,63497,424,62371,870,000998,0002,383,872
18Feb 7, 2025 12:43125,681102,604,49771,897,000998,0002,469,888
19Feb 7, 2025 12:37128,13696,428,55372,311,0002,008,0002,461,696
20Feb 7, 2025 12:37128,54199,939,19073,547,0001,007,0002,363,392
21Feb 7, 2025 12:38128,58394,010,12173,571,0001,007,0002,326,528
22Feb 7, 2025 12:43133,30798,589,02376,314,0001,004,0002,355,200
23Feb 7, 2025 12:37136,552105,464,57078,198,0001,002,0002,445,312
24Feb 7, 2025 12:38142,017106,531,47681,366,0001,004,0002,318,336
25Feb 7, 2025 12:37143,436107,980,81082,191,0001,002,0002,322,432