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 14:45__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 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
3Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
4Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
5Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
6Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
7Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
8Feb 15, 2024 14:5100000Error: expected "673943 ", got "677309"
9Feb 15, 2024 14:5100000Error: expected "935292 ", got "936924"
10Feb 15, 2024 14:5100000Error: expected "1300505 ", got "1300953"
11Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
12Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
13Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
14Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
15Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
16Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
17Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
18Feb 15, 2024 14:5000000Error: expected "687454 ", got "688021"
19Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
20Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
21Feb 15, 2024 14:5200000Error: expected "2697011 ", got "2695339"
22Feb 15, 2024 16:2100000Error: expected "3607066 ", got "3607792"
23Feb 15, 2024 16:2100000Error: expected "1152761 ", got "1152872"
24Feb 15, 2024 16:2100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
25Feb 15, 2024 16:2100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
26Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
27Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
28Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
29Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
30Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
31Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
32Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
33Feb 15, 2024 14:5200000Error: expected "1616595 ", got "1634041"
34Feb 15, 2024 14:5200000Error: expected "1849635 ", got "1853629"
35Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
36Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
37Feb 15, 2024 14:5200000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
38Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
39Feb 15, 2024 14:5100000Error: expected "742977 ", got "743058"
40Feb 15, 2024 14:5100000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
41Feb 15, 2024 14:4900000Error: expected "1406241 ", got "1424823"
42Feb 15, 2024 14:4900000Error: expected "2199147 ", got "2244526"
43Feb 15, 2024 14:4900000Error: expected "808798 ", got "807828"
44Feb 15, 2024 14:4900000Error: expected "4035969 ", got "4035665"
45Feb 15, 2024 14:4900000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
46Feb 15, 2024 14:4900000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
47Feb 15, 2024 14:4900000Error: expected "1835164 ", got "1837689"
48Feb 15, 2024 14:4900000Error: expected "833693 ", got "833461"
49Feb 15, 2024 14:4900000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
50Feb 15, 2024 14:4900000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
51Feb 15, 2024 14:5000000Error: expected "1672343 ", got "1669241"
52Feb 15, 2024 14:4900000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
53Feb 15, 2024 14:4900000Error: expected "1601022 ", got "1600838"
54Feb 15, 2024 14:4700000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
55Feb 15, 2024 14:4700000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
56Feb 15, 2024 14:4600000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
57Feb 15, 2024 14:4600000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
58Feb 15, 2024 14:4500000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
59Feb 15, 2024 14:4500000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
60Feb 15, 2024 14:4500000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
61Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
62Feb 15, 2024 14:5000000Error: expected "1100127 ", got "1102659"
63Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
64Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
65Feb 15, 2024 14:5000000Error: expected "2360081 ", got "2368142"
66Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
67Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
68Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
69Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
70Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
71Feb 15, 2024 14:4500000Error: expected "1209417 ", got "1207877"
72Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
73Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
74Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
75Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
76Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
77Feb 15, 2024 14:5000000Error: expected "1943578 ", got "1945396"
78Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
79Feb 15, 2024 14:5000000Error: Exit with code 101: thread 'main' panicked at main.rs:108:36: index out of bounds: the len is 67 but the index is 67 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr