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 17, 2023 05:20Kenneth MaplesKenneth MaplesScore: 90,322Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 17, 2023 05:2079,840108,925,96146,307,16002,297,856
2Dec 17, 2023 05:2079,896125,681,07046,339,40902,293,760
3Dec 17, 2023 05:2079,960102,401,75446,376,85402,301,952
4Dec 17, 2023 05:2089,97877,111,74750,180,0002,007,0002,326,528
5Dec 17, 2023 05:2090,13174,645,70851,271,0001,005,0002,322,432
6Dec 17, 2023 05:2090,27878,365,77052,361,00002,314,240
7Dec 17, 2023 05:2090,32280,098,61752,387,00002,318,336
8Dec 17, 2023 05:2090,83182,010,96552,682,00002,256,896
9Dec 17, 2023 05:2091,17980,215,53951,887,000997,0002,519,040
10Dec 17, 2023 05:2091,18478,515,70850,892,0001,995,0002,322,432
11Dec 17, 2023 05:2091,98179,183,62553,349,00002,519,040
12Dec 17, 2023 05:2092,04784,893,48553,387,00002,256,896