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 18:22HattonuriHattonuriScore: 26,328Success
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 18:2125,15929,932,79514,592,00002,506,752
2Dec 28, 2025 18:2125,72441,776,54913,926,000994,0002,322,432
3Dec 28, 2025 18:2125,74734,867,72712,942,0001,991,0002,510,848
4Dec 28, 2025 18:2126,09839,692,76213,119,0002,018,0002,273,280
5Dec 28, 2025 18:2126,13435,005,74214,148,0001,010,0002,383,872
6Dec 28, 2025 18:2126,29540,568,92414,235,0001,016,0002,273,280
7Dec 28, 2025 18:2126,32834,091,89713,234,0002,036,0002,326,528
8Dec 28, 2025 18:2126,42434,862,88715,326,00002,281,472
9Dec 28, 2025 18:2126,44539,780,11413,293,0002,045,0002,285,568
10Dec 28, 2025 18:2226,45732,442,66814,322,0001,023,0002,424,832
11Dec 28, 2025 18:2226,45930,291,57615,346,00002,269,184
12Dec 28, 2025 18:2226,46631,808,58015,350,00002,510,848