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 listJun 13, 2026 00:47Victor MerckléVictor MerckléScore: 36,639Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jun 13, 2026 00:5035,30029,362,59819,139,0001,335,0002,129,920
2Jun 13, 2026 00:5035,73129,666,50019,696,0001,028,0002,129,920
3Jun 13, 2026 00:5035,96729,780,38819,828,0001,033,0002,134,016
4Jun 13, 2026 00:5036,30130,030,48319,786,0001,269,0002,174,976
5Jun 13, 2026 00:5036,63930,139,97020,221,0001,030,0002,134,016
6Jun 13, 2026 00:5036,81730,288,83920,275,0001,079,0002,134,016
7Jun 13, 2026 00:5036,91030,354,49520,366,0001,042,0002,129,920
8Jun 13, 2026 00:5036,95030,326,57420,357,0001,074,0002,129,920
9Jun 13, 2026 00:5037,09830,490,48020,484,0001,033,0002,134,016