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 17:16E SequeiraE SequeiraScore: 82,867Success
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 17:1682,18170,751,63845,679,0001,986,0002,285,568
2Mar 26, 2024 17:1682,22874,288,21746,678,0001,014,0002,506,752
3Mar 26, 2024 17:1882,292116,139,64847,729,57902,211,840
4Mar 26, 2024 17:1882,30572,727,50747,737,00002,285,568
5Mar 26, 2024 17:1882,32974,075,14247,751,00002,510,848
6Mar 26, 2024 17:3582,525109,013,04847,864,75402,215,936
7Mar 26, 2024 17:1882,54773,279,71646,880,000997,0002,285,568
8Mar 26, 2024 17:3582,56077,515,13545,890,0001,995,0002,519,040
9Mar 26, 2024 17:3582,61970,540,74245,923,0001,996,0002,285,568
10Mar 26, 2024 17:3582,66769,340,22045,950,0001,997,0002,510,848
11Mar 26, 2024 17:3582,69076,978,43445,962,0001,998,0002,285,568
12Mar 26, 2024 17:3582,70275,643,54245,969,0001,998,0002,260,992
13Mar 26, 2024 17:3582,735120,559,69347,986,43302,207,744
14Mar 26, 2024 17:1682,74570,384,91345,993,0001,999,0002,506,752
15Mar 26, 2024 17:3582,74771,443,87446,994,000999,0002,285,568
16Mar 26, 2024 17:1682,760106,386,60348,000,80602,203,648
17Mar 26, 2024 17:1682,78999,953,12248,017,88702,207,744
18Mar 26, 2024 17:1682,80075,460,59347,024,0001,000,0002,506,752
19Mar 26, 2024 17:3582,867124,152,04748,062,77402,220,032
20Mar 26, 2024 17:1882,86974,801,72946,062,0002,002,0002,285,568
21Mar 26, 2024 17:3582,87671,773,31846,066,0002,002,0002,285,568
22Mar 26, 2024 17:1682,879104,907,48248,069,80902,211,840
23Mar 26, 2024 17:1682,88875,138,98746,072,0002,003,0002,285,568
24Mar 26, 2024 17:3582,91977,577,67146,090,0002,003,0002,285,568
25Mar 26, 2024 17:1682,95277,143,27246,108,0002,004,0002,510,848
26Mar 26, 2024 17:1883,01470,474,79246,142,0002,006,0002,285,568
27Mar 26, 2024 17:1883,116109,062,30848,207,15902,211,840
28Mar 26, 2024 17:3583,15073,110,06046,218,0002,009,0002,285,568
29Mar 26, 2024 17:1883,218106,365,35148,266,62002,211,840
30Mar 26, 2024 17:1883,37274,803,44447,349,0001,007,0002,273,280
31Mar 26, 2024 17:1883,56976,135,19448,470,00002,285,568
32Mar 26, 2024 17:1883,81474,226,62547,600,0001,012,0002,285,568
33Mar 26, 2024 17:1683,83669,220,20147,612,0001,013,0002,506,752
34Mar 26, 2024 17:1683,83676,360,94647,612,0001,013,0002,269,184
35Mar 26, 2024 17:1684,22277,029,96448,849,00002,506,752
36Mar 26, 2024 17:1884,29176,908,34246,894,0001,995,0002,285,568