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 listMar 14, 2026 16:45Dominique GarmierDominique GarmierScore: 82,681Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 14, 2026 16:4579,31679,903,39444,003,0002,000,0002,514,944
2Mar 14, 2026 16:4579,52173,747,28846,122,00002,506,752
3Mar 14, 2026 16:4579,52871,161,28344,121,0002,005,0002,260,992
4Mar 14, 2026 16:4580,33174,304,20246,592,00002,519,040
5Mar 14, 2026 16:4582,68173,049,37546,956,000999,0002,506,752
6Mar 14, 2026 16:4583,02176,585,45746,146,0002,006,0002,404,352
7Mar 14, 2026 16:4583,10276,868,35446,191,0002,008,0002,400,256
8Mar 14, 2026 16:4583,72976,422,77847,552,0001,011,0002,400,256
9Mar 14, 2026 16:4583,86678,316,28647,629,0001,013,0002,486,272