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 listOct 3, 2026 07:32Karthik ReddyKarthik ReddyError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 3, 2026 07:32038,007,57127,696,00002,113,536expected "777000 ", got "B8847980 a729511 d260048 b10441 p284-1660 s1-500 dmax835 dmean269 d0 475 bmax82095 balmax469464 bal469463 "
2Oct 3, 2026 07:32039,088,62728,252,00002,113,536expected "1618584 ", got "B9296134 a730656 d258685 b10659 p379-2749 s1-500 dmax792 dmean267 d0 498 bmax76222 balmax471971 bal471971 "
3Oct 3, 2026 07:32040,431,82328,147,00002,113,536expected "3143189 ", got "B9431248 a729192 d260257 b10551 p565-3744 s1-500 dmax781 dmean269 d0 483 bmax70758 balmax468935 bal468935 "