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 15, 2025 16:00Alexey IlyukhovAlexey IlyukhovScore: 73,650Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 15, 2025 15:4670,80562,642,30940,066,0001,001,0002,424,832
2Dec 15, 2025 15:4671,27462,828,51140,331,0001,008,0002,469,888
3Dec 15, 2025 15:4671,60363,486,21441,530,00002,453,504
4Dec 15, 2025 15:4671,88668,874,07240,678,0001,016,0002,404,352
5Dec 15, 2025 15:4672,24866,512,49540,907,000997,0002,461,696
6Dec 15, 2025 15:4673,59568,666,19242,685,00002,347,008
7Dec 15, 2025 16:0073,65069,196,84642,717,00002,318,336
8Dec 15, 2025 15:4675,02464,419,22043,514,00002,396,160
9Dec 15, 2025 15:4675,39167,864,08443,727,00002,404,352
10Dec 15, 2025 16:0075,68171,023,75243,895,00002,363,392
11Dec 15, 2025 16:0075,91470,274,63642,029,0002,001,0002,310,144
12Dec 15, 2025 15:4677,40767,452,95542,901,0001,995,0002,478,080