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 29, 2025 02:29Carl BergmanCarl BergmanScore: 249,690Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 02:29248,474169,821,417139,111,0005,004,0002,379,776
2Dec 29, 2025 02:29248,964179,428,430138,383,0006,016,0002,269,184
3Dec 29, 2025 02:29249,121169,886,263140,477,0004,013,0002,478,080
4Dec 29, 2025 02:29249,664171,863,971139,812,0004,993,0002,514,944
5Dec 29, 2025 02:29249,690175,492,334138,828,0005,992,0002,478,080
6Dec 29, 2025 02:29250,074177,163,582140,042,0005,001,0002,371,584
7Dec 29, 2025 02:29250,102174,016,133139,057,0006,002,0002,342,912
8Dec 29, 2025 02:29250,212171,748,194140,119,0005,004,0002,514,944
9Dec 29, 2025 02:29251,412174,806,556141,824,0003,995,0002,293,760