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 07:43Sergey StreminSergey StreminScore: 75,384Success
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:4373,94573,597,03541,891,000997,0002,338,816
2Dec 5, 2023 07:4774,107112,453,67242,981,97402,277,376
3Dec 5, 2023 07:4574,11070,134,91541,985,000999,0002,338,816
4Dec 5, 2023 07:4574,14872,290,96041,006,0002,000,0002,338,816
5Dec 5, 2023 07:4574,182103,802,65443,025,78802,281,472
6Dec 5, 2023 08:5674,22267,153,39841,047,0002,002,0002,338,816
7Dec 5, 2023 07:4374,23367,334,09041,053,0002,002,0002,289,664
8Dec 5, 2023 07:4374,23473,430,32442,055,0001,001,0002,289,664
9Dec 5, 2023 07:4374,23867,924,81141,056,0002,002,0002,289,664
10Dec 5, 2023 08:5674,29068,091,52242,086,0001,002,0002,289,664
11Dec 5, 2023 07:4774,33866,048,39141,111,0002,005,0002,334,720
12Dec 5, 2023 08:5674,42968,560,66341,162,0002,007,0002,338,816
13Dec 5, 2023 07:4774,445114,559,19843,178,02502,269,184
14Dec 5, 2023 08:5674,46771,063,27241,183,0002,008,0002,289,664
15Dec 5, 2023 08:5674,534110,877,34343,229,52902,285,568
16Dec 5, 2023 07:4374,564114,522,36243,246,92702,281,472
17Dec 5, 2023 07:4774,61768,246,74143,278,00002,330,624
18Dec 5, 2023 07:4574,81466,799,00043,392,00002,338,816
19Dec 5, 2023 07:4374,81769,101,10943,394,00002,289,664
20Dec 5, 2023 08:5674,97168,316,65142,472,0001,011,0002,289,664
21Dec 5, 2023 08:5675,018132,443,29243,510,32402,285,568
22Dec 5, 2023 07:4775,049113,047,00343,528,64802,289,664
23Dec 5, 2023 07:4775,26271,115,55543,652,00002,289,664
24Dec 5, 2023 07:4575,33375,671,40843,693,00002,330,624
25Dec 5, 2023 07:4775,38472,520,78543,723,00002,330,624
26Dec 5, 2023 07:4575,44368,070,43042,740,0001,017,0002,289,664
27Dec 5, 2023 08:5675,67268,783,18441,895,0001,995,0002,334,720
28Dec 5, 2023 07:4775,68367,612,58642,899,000997,0002,334,720
29Dec 5, 2023 07:4575,70068,405,91142,909,000997,0002,289,664
30Dec 5, 2023 07:4375,761116,092,48143,941,22902,277,376
31Dec 5, 2023 07:4375,81468,826,02541,974,0001,998,0002,334,720
32Dec 5, 2023 08:5675,91969,430,69342,032,0002,001,0002,289,664
33Dec 5, 2023 07:4775,97271,897,56543,063,0001,001,0002,334,720
34Dec 5, 2023 07:4375,99367,433,90242,073,0002,003,0002,334,720
35Dec 5, 2023 07:4576,02466,549,09542,090,0002,004,0002,289,664
36Dec 5, 2023 07:4576,02967,105,08042,093,0002,004,0002,289,664
37Dec 5, 2023 07:4376,042121,656,19444,104,52002,285,568
38Dec 5, 2023 07:4776,07966,285,25342,121,0002,005,0002,330,624
39Dec 5, 2023 07:4576,33691,917,29244,274,72902,289,664
40Dec 5, 2023 07:4576,46265,413,38242,333,0002,015,0002,289,664
41Dec 5, 2023 08:5676,610112,757,41944,433,95302,285,568
42Dec 5, 2023 07:4576,981114,078,62944,649,13102,260,992
43Dec 5, 2023 07:4377,37171,270,38143,878,000997,0002,334,720
44Dec 5, 2023 07:4377,49869,635,09442,952,0001,997,0002,289,664
45Dec 5, 2023 07:4777,84069,938,23044,144,0001,003,0002,289,664
46Dec 5, 2023 07:4778,03170,704,97944,253,0001,005,0002,289,664
47Dec 5, 2023 08:5679,45968,834,45744,083,0002,003,0002,334,720
48Dec 5, 2023 08:5680,98673,972,81445,973,000999,0002,289,664