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 27, 2024 15:25E SequeiraE SequeiraScore: 77,069Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 15:2675,78672,585,07141,958,0001,998,0002,510,848
2Mar 27, 2024 15:2675,97976,138,00542,065,0002,003,0002,285,568
3Mar 27, 2024 15:2676,04369,911,87643,103,0001,002,0002,285,568
4Mar 27, 2024 15:2676,10771,176,58842,136,0002,006,0002,285,568
5Mar 27, 2024 15:2576,25072,317,82942,215,0002,010,0002,285,568
6Mar 27, 2024 15:2676,30072,031,75542,243,0002,011,0002,285,568
7Mar 27, 2024 15:4976,40376,372,19843,307,0001,007,0002,285,568
8Mar 27, 2024 15:2576,47173,160,79643,345,0001,008,0002,285,568
9Mar 27, 2024 15:4976,52269,935,56444,383,00002,285,568
10Mar 27, 2024 15:2576,53368,421,61943,381,0001,008,0002,510,848
11Mar 27, 2024 15:2676,70970,731,75143,480,0001,011,0002,285,568
12Mar 27, 2024 15:4976,71671,149,00243,484,0001,011,0002,285,568
13Mar 27, 2024 15:2576,76073,076,81943,510,0001,011,0002,285,568
14Mar 27, 2024 15:4976,80267,594,84043,533,0001,012,0002,519,040
15Mar 27, 2024 15:2576,91667,608,06443,598,0001,013,0002,269,184
16Mar 27, 2024 15:4976,95569,416,26243,620,0001,014,0002,506,752
17Mar 27, 2024 15:4977,01774,931,53043,655,0001,015,0002,519,040
18Mar 27, 2024 15:2577,05072,466,07243,674,0001,015,0002,355,200
19Mar 27, 2024 15:2677,06970,443,25144,700,00002,519,040
20Mar 27, 2024 15:2577,11975,006,41143,713,0001,016,0002,519,040
21Mar 27, 2024 15:4977,39877,362,06942,896,0001,995,0002,285,568
22Mar 27, 2024 15:2577,40774,196,87443,899,000997,0002,269,184
23Mar 27, 2024 15:4977,46274,092,68442,932,0001,996,0002,260,992
24Mar 27, 2024 15:4977,52470,080,83642,966,0001,998,0002,519,040
25Mar 27, 2024 15:2677,61269,595,43443,015,0002,000,0002,260,992
26Mar 27, 2024 15:2577,76471,768,65743,099,0002,004,0002,519,040
27Mar 27, 2024 15:2677,80265,133,64143,120,0002,005,0002,281,472
28Mar 27, 2024 15:2579,395108,209,40546,049,17702,228,224
29Mar 27, 2024 15:2679,729101,193,37246,242,62402,224,128
30Mar 27, 2024 15:2680,220116,587,35146,527,83002,236,416
31Mar 27, 2024 15:4980,579113,806,10546,735,59802,228,224
32Mar 27, 2024 15:4980,668108,339,75946,787,21402,236,416
33Mar 27, 2024 15:2581,497118,080,95747,268,15202,232,320
34Mar 27, 2024 15:4981,566112,794,96747,308,43002,228,224
35Mar 27, 2024 15:2681,608111,812,53347,332,61502,236,416
36Mar 27, 2024 15:2581,828111,960,92947,460,04002,228,224