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 21:51E SequeiraE SequeiraScore: 83,241Success
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 21:4979,62673,283,87141,270,0004,913,0002,281,472
2Mar 26, 2024 21:5179,65075,929,27942,180,0004,017,0002,367,488
3Mar 26, 2024 21:4979,89569,877,40242,310,0004,029,0002,486,272
4Mar 26, 2024 21:4980,03673,342,43341,483,0004,938,0002,473,984
5Mar 26, 2024 21:5180,08169,432,67542,409,0004,038,0002,367,488
6Mar 26, 2024 21:5180,76674,470,19142,858,0003,986,0002,330,624
7Mar 26, 2024 21:4982,62267,155,36744,863,0003,058,0002,465,792
8Mar 26, 2024 21:4982,66674,566,24043,951,0003,995,0002,408,448
9Mar 26, 2024 21:4983,07978,645,82545,175,0003,011,0002,355,200
10Mar 26, 2024 21:4983,19177,342,13444,313,0003,938,0002,277,376
11Mar 26, 2024 21:4983,24175,197,65446,226,0002,054,0002,457,600
12Mar 26, 2024 21:4983,61469,535,86443,548,0004,948,0002,387,968
13Mar 26, 2024 21:4983,66077,071,33445,491,0003,032,0002,473,984
14Mar 26, 2024 21:4983,83173,165,51746,597,0002,025,0002,449,408
15Mar 26, 2024 21:4984,07172,134,86746,687,0002,074,0002,506,752
16Mar 26, 2024 21:4984,356127,632,47848,926,64502,252,800
17Mar 26, 2024 21:4984,635104,445,27849,088,38802,265,088
18Mar 26, 2024 21:4984,986108,962,01249,291,80802,281,472
19Mar 26, 2024 21:5185,143119,483,14049,382,66302,228,224
20Mar 26, 2024 21:5187,421109,245,26150,703,93802,269,184
21Mar 26, 2024 21:5188,089120,227,73451,091,89102,269,184