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 31, 2024 09:57Louis PonetLouis PonetError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 31, 2024 11:0100000Error: Exit with code 101: 503, 9 503, 10 503, 11 503, 12 503, 13 503, 14 503, 15 503, 16 503, 17 506, 9 503, 18 503, 19 503, 20 506, 10 503, 21 506, 11 503, 22 505, 9 503, 23 506, 12 503, 24 506, 13 503, 25 505, 10 503, 26 thread 'main' panicked at ma...stderr
2Dec 31, 2024 11:0100000Error: Exit with code 101: 503, 9 503, 10 503, 11 503, 12 503, 13 503, 14 503, 15 503, 16 503, 17 503, 18 503, 19 503, 20 503, 21 503, 22 503, 23 503, 24 503, 25 503, 24 1074, 9 thread 'main' panicked at main.rs:267:93: called `Option::unwrap()` on a `...stderr
3Dec 31, 2024 09:58105,03389,250,18159,921,000998,0002,371,584
4Dec 31, 2024 09:57105,13187,153,30559,977,000999,0002,514,944
5Dec 31, 2024 09:58105,29586,548,29759,069,0002,002,0002,359,296
6Dec 31, 2024 09:57105,65789,746,62161,281,00002,387,968
7Dec 31, 2024 09:58105,89185,819,54161,417,00002,281,472
8Dec 31, 2024 09:58105,93383,568,11261,441,00002,375,680
9Dec 31, 2024 09:57106,23489,903,48061,616,00002,482,176
10Dec 31, 2024 11:01106,31991,063,94661,665,00002,387,968
11Dec 31, 2024 09:57106,35989,550,27760,677,0001,011,0002,392,064
12Dec 31, 2024 11:02106,67288,707,38059,875,0001,995,0002,490,368
13Dec 31, 2024 09:57106,68691,749,69660,880,000998,0002,498,560
14Dec 31, 2024 11:02106,69788,826,44960,886,000998,0002,375,680
15Dec 31, 2024 09:57107,12991,974,77061,133,0001,002,0002,383,872
16Dec 31, 2024 09:58107,16689,620,33861,154,0001,002,0002,367,488
17Dec 31, 2024 11:02107,18389,485,36360,161,0002,005,0002,494,464
18Dec 31, 2024 11:01107,34387,349,85560,251,0002,008,0002,379,776
19Dec 31, 2024 09:58107,41090,886,42562,298,00002,383,872
20Dec 31, 2024 11:01108,87189,267,56562,143,0001,002,0002,506,752