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 26, 2025 12:18HattonuriHattonuriScore: 30,433Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 12:1028,82242,261,65416,717,00002,326,528
2Dec 26, 2025 12:1129,31035,522,02316,000,0001,000,0002,342,912
3Dec 26, 2025 12:1129,33338,761,77816,013,0001,000,0002,310,144
4Dec 26, 2025 12:1829,53436,656,48015,115,0002,015,0002,314,240
5Dec 26, 2025 12:1029,58438,410,15216,150,0001,009,0002,506,752
6Dec 26, 2025 12:1830,36638,704,94817,612,00002,502,656
7Dec 26, 2025 12:1830,43339,039,60717,651,00002,510,848
8Dec 26, 2025 12:1830,57244,992,00217,732,00002,400,256
9Dec 26, 2025 12:1830,86045,040,12414,916,0002,983,0002,478,080
10Dec 26, 2025 12:1130,87145,644,56515,916,0001,989,0002,510,848
11Dec 26, 2025 12:1830,99847,521,26316,981,000998,0002,375,680
12Dec 26, 2025 12:1031,57846,123,22218,315,00002,387,968