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 12:46E SequeiraE SequeiraScore: 78,060Success
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 12:4676,72268,883,84143,488,0001,011,0002,510,848
2Mar 27, 2024 12:4676,84571,235,38044,570,00002,510,848
3Mar 27, 2024 12:4676,99070,501,38944,654,00002,519,040
4Mar 27, 2024 12:4677,22869,607,36642,802,0001,990,0002,285,568
5Mar 27, 2024 12:4677,39169,698,07642,892,0001,995,0002,285,568
6Mar 27, 2024 12:4677,40275,269,98743,896,000997,0002,285,568
7Mar 27, 2024 12:4677,40774,509,04442,901,0001,995,0002,285,568
8Mar 27, 2024 12:4677,48368,307,92343,942,000998,0002,506,752
9Mar 27, 2024 12:4677,51067,877,64543,957,000999,0002,510,848
10Mar 27, 2024 12:4677,66464,495,07043,043,0002,002,0002,285,568
11Mar 27, 2024 12:4677,75366,351,06843,093,0002,004,0002,285,568
12Mar 27, 2024 12:4678,00766,697,37143,234,0002,010,0002,285,568
13Mar 27, 2024 12:4678,06071,217,31643,263,0002,012,0002,510,848
14Mar 27, 2024 12:4678,22865,394,62944,364,0001,008,0002,510,848
15Mar 27, 2024 12:4678,46668,846,02545,510,00002,285,568
16Mar 27, 2024 12:4678,46665,577,09544,499,0001,011,0002,506,752
17Mar 27, 2024 12:4678,54574,995,00145,556,00002,285,568
18Mar 27, 2024 12:4678,68174,058,65244,621,0001,014,0002,285,568
19Mar 27, 2024 12:4680,018109,003,73046,410,43302,224,128
20Mar 27, 2024 12:4680,467121,006,51046,671,08002,220,032
21Mar 27, 2024 12:4680,731105,009,08146,824,24002,220,032
22Mar 27, 2024 12:4680,996100,882,66246,977,95602,236,416
23Mar 27, 2024 12:4681,501108,592,57247,270,32602,224,128
24Mar 27, 2024 12:4681,771109,464,40347,427,02202,228,224