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 18:17Louis 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 18:1700000Error: Exit with code 101: thread 'main' panicked at main.rs:379:35: index out of bounds: the len is 4005 but the index is 4005 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Dec 31, 2024 18:1798,54178,674,28255,149,0002,005,0002,269,184
3Dec 31, 2024 18:1798,71779,417,76156,252,0001,004,0002,265,088
4Dec 31, 2024 18:1799,23385,544,11256,546,0001,009,0002,269,184
5Dec 31, 2024 18:1799,31282,013,84756,608,000993,0002,273,280
6Dec 31, 2024 18:17101,65384,630,87156,961,0001,998,0002,285,568
7Dec 31, 2024 18:17102,52182,524,94658,455,0001,007,0002,273,280
8Dec 31, 2024 18:17104,00989,162,27659,320,0001,005,0002,277,376
9Dec 31, 2024 18:17104,41684,285,62659,552,0001,009,0002,269,184
10Dec 31, 2024 18:17104,55088,343,72659,629,0001,010,0002,289,664