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 5, 2023 09:16Sergey StreminSergey StreminScore: 81,145Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 5, 2023 09:1680,833116,969,45846,883,16202,314,240
2Dec 5, 2023 09:1680,899109,034,37846,921,59302,306,048
3Dec 5, 2023 09:1680,97676,969,90545,967,000999,0002,359,296
4Dec 5, 2023 09:1680,99373,052,61545,977,000999,0002,363,392
5Dec 5, 2023 09:1681,11269,899,62645,044,0002,001,0002,363,392
6Dec 5, 2023 09:1681,13868,875,13946,059,0001,001,0002,281,472
7Dec 5, 2023 09:1681,14567,844,29245,062,0002,002,0002,281,472
8Dec 5, 2023 09:1681,205135,772,66547,099,12202,314,240
9Dec 5, 2023 09:1681,32472,115,52446,165,0001,003,0002,281,472
10Dec 5, 2023 09:1681,41268,497,54845,210,0002,009,0002,465,792
11Dec 5, 2023 09:1681,41770,723,17446,218,0001,004,0002,359,296
12Dec 5, 2023 09:1681,83167,402,28947,462,00002,281,472