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 21, 2025 07:18Liquidity GoblinLiquidity GoblinScore: 88,100Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 07:1887,89180,359,92547,979,0002,998,0002,297,856
2Dec 21, 2025 07:1887,91774,561,58447,993,0002,999,0002,457,600
3Dec 21, 2025 07:1887,94083,442,50449,005,0002,000,0002,445,312
4Dec 21, 2025 07:1888,01679,674,12248,047,0003,002,0002,469,888
5Dec 21, 2025 07:1888,10074,424,74149,095,0002,003,0002,420,736
6Dec 21, 2025 07:1888,21672,184,65350,162,0001,003,0002,269,184
7Dec 21, 2025 07:1888,29373,666,57548,198,0003,012,0002,478,080
8Dec 21, 2025 07:1888,35577,886,05548,232,0003,014,0002,379,776
9Dec 21, 2025 07:1888,38174,293,68646,236,0005,025,0002,441,216