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 listAug 13, 2025 12:41CadovvlCadovvlScore: 70,522Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 13, 2025 12:4169,29566,237,37238,182,0002,009,0002,510,848
2Aug 13, 2025 12:4169,86770,067,00639,510,0001,013,0002,519,040
3Aug 13, 2025 12:4069,92863,878,82640,558,00002,379,776
4Aug 13, 2025 12:4069,99561,794,28639,583,0001,014,0002,510,848
5Aug 13, 2025 12:4070,03161,288,20640,618,00002,256,896
6Aug 13, 2025 12:4070,15772,115,32139,674,0001,017,0002,510,848
7Aug 13, 2025 12:4170,21474,099,86539,706,0001,018,0002,387,968
8Aug 13, 2025 12:4070,51764,891,48540,900,00002,383,872
9Aug 13, 2025 12:4170,51769,667,23739,903,000997,0002,519,040
10Aug 13, 2025 12:4070,52268,445,73438,908,0001,995,0002,387,968
11Aug 13, 2025 12:4170,53168,816,53438,913,0001,995,0002,379,776
12Aug 13, 2025 12:4070,61263,549,17139,957,000998,0002,379,776
13Aug 13, 2025 12:4070,76774,761,20440,044,0001,001,0002,519,040
14Aug 13, 2025 12:4170,76768,992,06139,043,0002,002,0002,387,968
15Aug 13, 2025 12:4170,78365,158,48339,052,0002,002,0002,375,680
16Aug 13, 2025 12:4170,84568,568,68339,086,0002,004,0002,519,040
17Aug 13, 2025 12:4170,85766,360,97340,095,0001,002,0002,387,968
18Aug 13, 2025 12:4071,25063,151,14640,318,0001,007,0002,506,752