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 11, 2026 13:10Code HintsCode HintsScore: 43,450Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 10, 2026 18:2540,96653,833,81122,727,0001,033,0002,510,848
2Feb 10, 2026 18:2541,20549,042,67222,904,000995,0002,494,464
3Feb 10, 2026 18:2541,54046,489,09223,090,0001,003,0002,498,560
4Feb 10, 2026 18:2542,52858,422,75723,639,0001,027,0002,506,752
5Feb 11, 2026 13:1042,56749,662,55923,661,0001,028,0002,506,752
6Feb 11, 2026 13:1042,65052,913,98823,707,0001,030,0002,486,272
7Feb 10, 2026 18:2543,02149,977,65422,956,0001,996,0002,334,720
8Feb 10, 2026 18:2543,39345,331,16223,155,0002,013,0002,482,176
9Feb 11, 2026 13:1043,43444,584,98024,185,0001,007,0002,482,176
10Feb 11, 2026 13:1043,45047,902,83323,185,0002,016,0002,473,984
11Feb 10, 2026 18:2544,11639,483,78524,564,0001,023,0002,482,176
12Feb 10, 2026 18:2545,04151,328,86224,115,0002,009,0002,469,888
13Feb 11, 2026 13:1045,09747,740,85224,144,0002,012,0002,465,792
14Feb 11, 2026 13:1045,85247,857,70726,594,00002,473,984
15Feb 11, 2026 13:1045,89752,797,76525,597,0001,023,0002,482,176
16Feb 10, 2026 18:2546,70752,006,09326,087,0001,003,0002,420,736
17Feb 11, 2026 13:1047,19149,786,26527,371,00002,469,888
18Feb 11, 2026 13:1048,10759,303,81625,909,0001,993,0002,478,080