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 09:26E SequeiraE SequeiraScore: 79,410Success
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 09:2675,76967,102,77341,949,0001,997,0002,281,472
2Mar 27, 2024 09:2676,00066,587,32643,079,0001,001,0002,506,752
3Mar 27, 2024 09:2676,05767,909,13842,108,0002,005,0002,510,848
4Mar 27, 2024 09:2676,18369,099,77342,178,0002,008,0002,519,040
5Mar 27, 2024 09:2676,23870,202,74143,214,0001,004,0002,506,752
6Mar 27, 2024 09:2676,27667,834,24642,230,0002,010,0002,281,472
7Mar 27, 2024 09:2676,64566,715,97043,444,0001,010,0002,281,472
8Mar 27, 2024 09:2676,72273,335,18143,488,0001,011,0002,510,848
9Mar 27, 2024 09:2676,98370,010,34144,650,00002,363,392
10Mar 27, 2024 09:2679,40968,721,39844,055,0002,002,0002,285,568
11Mar 27, 2024 09:2679,41070,004,75344,056,0002,002,0002,510,848
12Mar 27, 2024 09:2679,47674,788,35044,092,0002,004,0002,269,184
13Mar 27, 2024 09:2679,63866,455,09844,182,0002,008,0002,285,568
14Mar 27, 2024 09:2679,75068,061,02444,244,0002,011,0002,510,848
15Mar 27, 2024 09:2680,00367,741,66645,394,0001,008,0002,519,040
16Mar 27, 2024 09:2680,45376,755,07845,649,0001,014,0002,285,568
17Mar 27, 2024 09:2680,85572,629,13244,901,0001,995,0002,506,752
18Mar 27, 2024 09:2680,91273,767,79544,932,0001,997,0002,285,568
19Mar 27, 2024 09:2683,110111,197,41348,203,61102,240,512
20Mar 27, 2024 09:2683,358128,181,15248,347,46502,228,224
21Mar 27, 2024 09:2684,190109,544,42948,829,97702,211,840