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 26, 2024 16:52E SequeiraE SequeiraScore: 82,614Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 26, 2024 16:5282,28673,016,89147,726,00002,510,848
2Mar 26, 2024 16:5282,34373,330,59346,743,0001,016,0002,273,280
3Mar 26, 2024 16:5282,58679,006,10546,903,000997,0002,273,280
4Mar 26, 2024 16:5282,58879,157,16745,906,0001,995,0002,273,280
5Mar 26, 2024 16:5282,59376,495,03645,908,0001,996,0002,506,752
6Mar 26, 2024 16:5282,60277,586,38545,913,0001,996,0002,506,752
7Mar 26, 2024 16:5282,614108,440,20847,916,07402,252,800
8Mar 26, 2024 16:5282,68173,090,52446,956,000999,0002,285,568
9Mar 26, 2024 16:5282,79771,588,67046,022,0002,000,0002,285,568
10Mar 26, 2024 16:5282,81072,156,10046,029,0002,001,0002,285,568
11Mar 26, 2024 16:5282,973105,621,06248,124,26702,211,840
12Mar 26, 2024 16:5283,142103,099,34248,222,40702,215,936