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 14, 2023 06:46Sergey StreminSergey StreminScore: 76,096Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 14, 2023 06:4674,90271,003,78543,443,00002,281,472
2Dec 14, 2023 06:4974,94765,711,61542,459,0001,010,0002,351,104
3Dec 14, 2023 06:4674,98164,673,95043,489,00002,482,176
4Dec 14, 2023 06:4774,99163,304,29942,484,0001,011,0002,453,504
5Dec 14, 2023 06:4775,194113,807,86043,612,69402,297,856
6Dec 14, 2023 06:4675,22468,320,74342,616,0001,014,0002,473,984
7Dec 14, 2023 06:4975,602124,199,32443,849,30302,297,856
8Dec 14, 2023 06:4675,71069,653,41343,912,00002,355,200
9Dec 14, 2023 06:4775,728115,200,29643,922,11402,297,856
10Dec 14, 2023 06:4675,85271,548,42141,995,0001,999,0002,281,472
11Dec 14, 2023 06:4775,85574,509,31341,997,0001,999,0002,355,200
12Dec 14, 2023 06:4675,883107,735,31044,012,39702,293,760
13Dec 14, 2023 06:4975,89773,311,15743,020,0001,000,0002,355,200
14Dec 14, 2023 06:4775,94070,032,94243,044,0001,001,0002,281,472
15Dec 14, 2023 06:4775,95270,631,09942,050,0002,002,0002,355,200
16Dec 14, 2023 06:4675,986103,043,87644,072,16302,293,760
17Dec 14, 2023 06:4676,019117,665,88444,090,97002,297,856
18Dec 14, 2023 06:4676,02266,998,45143,091,0001,002,0002,347,008
19Dec 14, 2023 06:4976,096113,888,06944,135,96102,256,896
20Dec 14, 2023 06:4976,119110,901,48544,149,00702,289,664
21Dec 14, 2023 06:4676,22869,024,91442,203,0002,009,0002,355,200
22Dec 14, 2023 06:4776,31764,996,64543,258,0001,006,0002,281,472
23Dec 14, 2023 06:4776,58668,013,93544,420,00002,281,472
24Dec 14, 2023 06:4776,71071,365,80843,481,0001,011,0002,445,312
25Dec 14, 2023 06:4976,78867,580,03543,525,0001,012,0002,281,472
26Dec 14, 2023 06:4676,81666,792,01344,553,00002,281,472
27Dec 14, 2023 06:4976,82171,044,54843,544,0001,012,0002,355,200
28Dec 14, 2023 06:4977,07269,130,39844,702,00002,281,472
29Dec 14, 2023 06:4777,65572,905,49243,039,0002,001,0002,281,472
30Dec 14, 2023 06:4778,155111,469,30545,329,66902,256,896
31Dec 14, 2023 06:4978,17466,211,90345,341,00002,347,008
32Dec 14, 2023 06:4678,32170,451,07544,417,0001,009,0002,281,472
33Dec 14, 2023 06:4979,90271,641,67546,343,00002,281,472
34Dec 14, 2023 06:4979,97167,775,40145,375,0001,008,0002,338,816
35Dec 14, 2023 06:4982,61774,290,14646,920,000998,0002,281,472
36Dec 14, 2023 06:4783,51474,580,56948,438,00002,281,472