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 17, 2024 12:35@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 17, 2024 12:3500000Error: expected "1829163 ", got "8773284 "
2Oct 17, 2024 12:3500000Error: expected "1081950 ", got "1137407 "
3Oct 17, 2024 12:3500000Error: expected "1388319 ", got "2488762 "
4Oct 17, 2024 12:3500000Error: expected "1457131 ", got "1458461 "
5Oct 17, 2024 12:3500000Error: expected "1284064 ", got "1906817 "
6Oct 17, 2024 12:3500000Error: expected "1901147 ", got "1903503 "
7Oct 17, 2024 12:3500000Error: expected "1959581 ", got "1961031 "
8Oct 17, 2024 12:3500000Error: expected "875064 ", got "6718164 "
9Oct 17, 2024 12:3500000Error: expected "1604794 ", got "1613531 "
10Oct 17, 2024 12:3500000Error: expected "881330 ", got "914903 "
11Oct 17, 2024 12:3500000Error: expected "931223 ", got "968886 "
12Oct 17, 2024 12:35336,376217,671,586191,096,0004,002,0002,469,888
13Oct 17, 2024 12:35340,474225,646,145193,466,0004,009,0002,334,720
14Oct 17, 2024 12:35341,371224,343,965193,996,0003,999,0002,465,792
15Oct 17, 2024 12:35341,488225,435,361193,062,0005,001,0002,469,888
16Oct 17, 2024 12:35346,431224,714,110197,932,0002,998,0002,404,352
17Oct 17, 2024 12:35349,262228,154,575200,567,0002,005,0002,469,888