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 listMar 26, 2024 21:29E SequeiraE SequeiraError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 26, 2024 21:3300000Error: expected "975699 ", got "977302 "
2Mar 26, 2024 21:3200000Error: expected "1447029 ", got "1455615 "
3Mar 26, 2024 21:3000000Error: expected "2377022 ", got "2379195 "
4Mar 26, 2024 21:3000000Error: expected "2241592 ", got "2239775 "
5Mar 26, 2024 21:3375,850106,675,95243,993,11402,240,512
6Mar 26, 2024 21:3376,56297,535,30144,405,86902,240,512
7Mar 26, 2024 21:2977,393113,644,99344,887,89802,232,320
8Mar 26, 2024 21:3177,483107,622,28344,939,96502,240,512
9Mar 26, 2024 21:3477,96597,387,15845,219,58502,240,512
10Mar 26, 2024 21:2978,283114,803,09045,404,40602,248,704
11Mar 26, 2024 21:3478,349106,565,49945,442,19002,244,608
12Mar 26, 2024 21:3178,678108,794,06745,633,22102,252,800
13Mar 26, 2024 21:3479,08399,996,70345,867,86102,244,608
14Mar 26, 2024 21:2979,503105,464,54346,111,52902,228,224
15Mar 26, 2024 21:3479,759118,985,57246,260,10602,240,512
16Mar 26, 2024 21:3479,996106,010,95346,397,54502,244,608
17Mar 26, 2024 21:3380,009101,071,52146,405,11902,240,512
18Mar 26, 2024 21:3181,159109,475,49547,072,02002,240,512
19Mar 26, 2024 21:3481,581115,168,49147,316,82702,248,704
20Mar 26, 2024 21:3182,313124,949,31347,741,65102,232,320
21Mar 26, 2024 21:3182,446116,197,52147,818,65902,236,416
22Mar 26, 2024 21:3183,019132,332,35448,150,85902,248,704