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 27, 2024 12:47E SequeiraE SequeiraScore: 78,572Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 12:4776,98677,115,28444,652,00002,285,568
2Mar 27, 2024 12:4777,06072,143,38144,695,00002,273,280
3Mar 27, 2024 12:4777,06973,393,01443,685,0001,015,0002,285,568
4Mar 27, 2024 12:4777,40975,924,95442,902,0001,995,0002,269,184
5Mar 27, 2024 12:4777,59870,578,58943,007,0002,000,0002,285,568
6Mar 27, 2024 12:4777,84066,002,39243,141,0002,006,0002,510,848
7Mar 27, 2024 12:4778,57266,521,58344,560,0001,012,0002,285,568
8Mar 27, 2024 12:4778,63374,839,21944,594,0001,013,0002,506,752
9Mar 27, 2024 12:4779,11280,271,75143,890,0001,995,0002,285,568
10Mar 27, 2024 12:4780,510118,465,15846,695,94502,220,032
11Mar 27, 2024 12:4781,079110,364,98347,025,91502,224,128
12Mar 27, 2024 12:4781,530110,119,06247,287,47102,224,128