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 listJan 30, 2026 14:27olliecrowolliecrowScore: 97,326Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 30, 2026 14:2795,26283,722,46349,225,0006,027,0002,510,848
2Jan 30, 2026 14:2796,35281,947,15350,895,0004,989,0002,527,232
3Jan 30, 2026 14:2796,63883,822,10250,045,0006,005,0002,514,944
4Jan 30, 2026 14:2796,69088,727,51750,072,0006,008,0002,363,392
5Jan 30, 2026 14:2797,32681,742,16551,409,0005,040,0002,400,256
6Jan 30, 2026 14:2798,07984,418,45349,900,0006,986,0002,408,448
7Jan 30, 2026 14:2798,71984,543,82152,235,0005,022,0002,297,856
8Jan 30, 2026 14:2799,79089,535,27352,889,0004,989,0002,482,176
9Jan 30, 2026 14:27100,03885,194,12752,020,0006,002,0002,387,968