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 listOct 15, 2024 21:37@drytecc@dryteccError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 15, 2024 21:3800000Error: expected "670221 ", got "669876 "
2Oct 15, 2024 21:3800000Error: expected "1270061 ", got "1270518 "
3Oct 15, 2024 21:3800000Error: expected "915005 ", got "915320 "
4Oct 15, 2024 21:3800000Error: expected "600005 ", got "603138 "
5Oct 15, 2024 21:3800000Error: expected "725394 ", got "726344 "
6Oct 15, 2024 21:3800000Error: expected "926478 ", got "937768 "
7Oct 15, 2024 21:3800000Error: expected "1557540 ", got "1570194 "
8Oct 15, 2024 21:3800000Error: expected "1686654 ", got "1693391 "
9Oct 15, 2024 21:3800000Error: expected "1927991 ", got "2063802 "
10Oct 15, 2024 21:3800000Error: expected "1515542 ", got "1818516 "
11Oct 15, 2024 21:3700000Error: expected "3629698 ", got "3629141 "
12Oct 15, 2024 21:3700000Error: expected "1350125 ", got "1733541 "
13Oct 15, 2024 21:3700000Error: expected "1723061 ", got "1778531 "
14Oct 15, 2024 21:3700000Error: expected "1812683 ", got "1812474 "
15Oct 15, 2024 21:3700000Error: expected "1713941 ", got "1712213 "
16Oct 15, 2024 21:3700000Error: expected "1520214 ", got "1551975 "
17Oct 15, 2024 21:3857,48864,002,07230,312,0003,031,0002,396,160
18Oct 15, 2024 21:3857,91259,954,80531,554,0002,035,0002,330,624
19Oct 15, 2024 21:3858,39562,620,61730,881,0002,988,0002,265,088