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 13:21@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 13:2100000Error: expected "785258 ", got "786804 "
2Oct 17, 2024 13:2100000Error: expected "1076981 ", got "1073510 "
3Oct 17, 2024 13:2100000Error: expected "1828880 ", got "1829983 "
4Oct 17, 2024 13:2100000Error: expected "922756 ", got "940896 "
5Oct 17, 2024 13:2100000Error: expected "2312364 ", got "2310562 "
6Oct 17, 2024 13:2100000Error: expected "1016869 ", got "1017289 "
7Oct 17, 2024 13:2100000Error: expected "2684151 ", got "2692276 "
8Oct 17, 2024 13:2131,96240,857,39315,449,0003,089,0002,465,792
9Oct 17, 2024 13:2132,04142,554,54615,487,0003,097,0002,314,240
10Oct 17, 2024 13:2132,11034,993,44316,555,0002,069,0002,506,752
11Oct 17, 2024 13:2132,13335,913,83216,567,0002,070,0002,494,464
12Oct 17, 2024 13:2132,25544,677,09816,630,0002,078,0002,478,080
13Oct 17, 2024 13:2132,31049,925,03717,699,0001,041,0002,457,600
14Oct 17, 2024 13:2132,34544,953,56517,718,0001,042,0002,469,888
15Oct 17, 2024 13:2132,54846,263,50416,891,0001,987,0002,502,656
16Oct 17, 2024 13:2132,55544,309,64416,895,0001,987,0002,469,888
17Oct 17, 2024 13:2132,56945,977,71816,902,0001,988,0002,465,792
18Oct 17, 2024 13:2132,59049,349,88215,918,0002,984,0002,498,560
19Oct 17, 2024 13:2132,66045,762,45015,952,0002,991,0002,461,696
20Oct 17, 2024 13:2132,75345,370,27815,998,0002,999,0002,461,696
21Oct 17, 2024 13:2132,81436,553,30116,027,0003,005,0002,469,888
22Oct 17, 2024 13:2132,83839,264,38016,039,0003,007,0002,351,104
23Oct 17, 2024 13:2132,95045,607,13615,088,0004,023,0002,437,120
24Oct 17, 2024 13:2133,11247,551,94915,162,0004,043,0002,289,664
25Oct 17, 2024 13:2133,27450,185,83717,268,0002,031,0002,461,696