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 listApr 10, 2025 17:21yenw0dyenw0dError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 10, 2025 17:2100000Error: Exit with code 101: thread 'main' panicked at main.rs:109:9: assertion failed: index < self.size note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Apr 10, 2025 17:21117,44094,969,39267,114,0001,001,0002,400,256
3Apr 10, 2025 17:21117,56490,112,02967,185,0001,002,0002,506,752
4Apr 10, 2025 17:21117,88188,252,07568,371,00002,285,568
5Apr 10, 2025 17:21118,040102,011,82968,463,00002,297,856
6Apr 10, 2025 17:21118,38697,205,72367,655,0001,009,0002,314,240
7Apr 10, 2025 17:21118,72496,406,52367,863,000997,0002,400,256
8Apr 10, 2025 17:21118,75299,188,64466,880,0001,996,0002,457,600
9Apr 10, 2025 17:21118,84194,969,72867,930,000998,0002,355,200
10Apr 10, 2025 17:21119,06091,359,69767,054,0002,001,0002,445,312