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 listFeb 23, 2026 11:49Jem BishopJem BishopScore: 52,528Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 23, 2026 11:4950,14058,330,51624,067,0005,014,0002,461,696
2Feb 23, 2026 11:4950,56058,933,19624,269,0005,056,0002,510,848
3Feb 23, 2026 11:4951,51757,829,98623,904,0005,976,0002,473,984
4Feb 23, 2026 11:4952,04857,113,24325,157,0005,031,0002,269,184
5Feb 23, 2026 11:4952,52856,540,83725,389,0005,077,0002,514,944
6Feb 23, 2026 11:4953,57161,815,28627,062,0004,009,0002,379,776
7Feb 23, 2026 11:4953,70259,035,09226,124,0005,023,0002,334,720
8Feb 23, 2026 11:4954,57856,860,52227,571,0004,084,0002,400,256
9Feb 23, 2026 11:4954,99363,305,49825,916,0005,980,0002,445,312