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 listApr 27, 2023 10:00unsatcoreunsatcoreScore: 89,519Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 27, 2023 10:0085,08472,935,52948,342,0001,007,0002,478,080
2Apr 27, 2023 10:0085,64082,438,91446,630,0003,041,0002,449,408
3Apr 27, 2023 10:0085,76676,133,46146,760,0002,984,0002,469,888
4Apr 27, 2023 10:0085,84876,882,09445,809,0003,983,0002,318,336
5Apr 27, 2023 10:0087,25382,651,00547,571,0003,036,0002,297,856
6Apr 27, 2023 10:0089,49878,776,42149,913,0001,996,0002,326,528
7Apr 27, 2023 10:0089,51980,008,22448,926,0002,995,0002,310,144
8Apr 27, 2023 10:0091,49179,883,28851,063,0002,002,0002,469,888
9Apr 27, 2023 10:0093,02882,040,62451,958,0001,998,0002,306,048
10Apr 27, 2023 10:0097,451117,364,00156,521,37302,232,320
11Apr 27, 2023 10:0098,098123,818,38156,896,59302,211,840
12Apr 27, 2023 10:0098,275120,824,36756,999,74002,228,224