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 15, 2024 19:00__Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 15, 2024 19:0000000Error: No answer to stdout. Died or killed?
2Feb 15, 2024 19:0100000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8660337 but the index is 8660337 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
3Feb 15, 2024 19:0100000Error: No answer to stdout. Died or killed?
4Feb 15, 2024 19:0100000Error: No answer to stdout. Died or killed?
5Feb 15, 2024 19:0100000Error: No answer to stdout. Died or killed?
6Feb 15, 2024 19:0100000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8454536 but the index is 8454536 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
7Feb 15, 2024 19:0100000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8556422 but the index is 8556422 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
8Feb 15, 2024 19:0100000Error: No answer to stdout. Died or killed?
9Feb 15, 2024 19:0100000Error: No answer to stdout. Died or killed?
10Feb 15, 2024 19:0100000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8542684 but the index is 8542684 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
11Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8490107 but the index is 8490107 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
12Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8541206 but the index is 8541206 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
13Feb 15, 2024 19:0000000Error: No answer to stdout. Died or killed?
14Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8644010 but the index is 8644010 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
15Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8635258 but the index is 8635258 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
16Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8581559 but the index is 8581559 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
17Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8486192 but the index is 8486192 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
18Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8584656 but the index is 8584656 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
19Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8601493 but the index is 8601493 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
20Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8619085 but the index is 8619085 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
21Feb 15, 2024 19:0000000Error: No answer to stdout. Died or killed?
22Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8625625 but the index is 8625625 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
23Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8528315 but the index is 8528315 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
24Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8592253 but the index is 8592253 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
25Feb 15, 2024 19:0000000Error: Exit with code 101: thread 'main' panicked at main.rs:66:12: index out of bounds: the len is 8664153 but the index is 8664153 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr