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 28, 2025 17:35Josu San MartinJosu San MartinScore: 36,347Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 17:3534,30244,118,21618,901,000994,0002,318,336
2Dec 28, 2025 17:3535,05347,396,77720,331,00002,478,080
3Dec 28, 2025 17:3535,38843,142,17520,525,00002,277,376
4Dec 28, 2025 17:3535,39143,735,06520,527,00002,510,848
5Dec 28, 2025 17:3536,34744,808,96719,074,0002,007,0002,375,680
6Dec 28, 2025 17:3536,41948,483,61519,112,0002,011,0002,392,064
7Dec 28, 2025 17:3536,60045,282,25519,207,0002,021,0002,514,944
8Dec 28, 2025 17:3537,08650,283,28621,510,00002,314,240
9Dec 28, 2025 17:3537,19547,129,86020,546,0001,027,0002,301,952