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 06:58Sergey StreminSergey StreminScore: 77,850Success
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 07:2876,300113,486,03444,253,81802,293,760
2Dec 5, 2023 06:5876,373127,049,00144,296,22202,306,048
3Dec 5, 2023 07:2876,889117,228,86844,595,71302,297,856
4Dec 5, 2023 06:5876,95968,803,85644,636,00002,289,664
5Dec 5, 2023 07:2877,05375,151,40143,676,0001,015,0002,289,664
6Dec 5, 2023 06:5877,196104,404,51544,773,79602,285,568
7Dec 5, 2023 07:2877,49574,199,23543,949,000998,0002,289,664
8Dec 5, 2023 07:2877,52870,905,79642,968,0001,998,0002,281,472
9Dec 5, 2023 06:5877,67866,689,86043,051,0002,002,0002,289,664
10Dec 5, 2023 06:5877,71265,863,72143,070,0002,003,0002,449,408
11Dec 5, 2023 07:2877,73668,060,88943,084,0002,003,0002,355,200
12Dec 5, 2023 07:2877,842121,167,91545,148,29602,301,952
13Dec 5, 2023 06:5877,85069,699,94644,150,0001,003,0002,289,664
14Dec 5, 2023 07:2877,87667,132,04443,161,0002,007,0002,289,664
15Dec 5, 2023 06:5878,390116,235,96545,465,98202,301,952
16Dec 5, 2023 06:5878,45066,498,32145,501,00002,289,664
17Dec 5, 2023 06:5878,48467,844,42945,521,00002,351,104
18Dec 5, 2023 07:2878,49766,523,84244,517,0001,011,0002,355,200
19Dec 5, 2023 07:2878,70978,588,54344,637,0001,014,0002,359,296
20Dec 5, 2023 07:2878,84375,748,35844,713,0001,016,0002,289,664
21Dec 5, 2023 07:2879,36771,700,53845,033,0001,000,0002,289,664
22Dec 5, 2023 06:5880,82878,607,99146,880,00002,347,008
23Dec 5, 2023 06:5881,17170,033,89845,076,0002,003,0002,289,664
24Dec 5, 2023 06:5881,89369,368,98547,498,00002,355,200