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 20:41E SequeiraE SequeiraScore: 81,274Success
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 20:4179,65567,331,36844,192,0002,008,0002,510,848
2Mar 27, 2024 20:4179,69073,232,64044,211,0002,009,0002,285,568
3Mar 27, 2024 20:4480,06773,715,46046,439,00002,510,848
4Mar 27, 2024 20:4480,35069,883,37345,590,0001,013,0002,510,848
5Mar 27, 2024 20:4180,42978,044,10345,635,0001,014,0002,510,848
6Mar 27, 2024 20:4480,82872,576,22844,886,0001,994,0002,285,568
7Mar 27, 2024 20:4180,84075,441,23746,887,00002,273,280
8Mar 27, 2024 20:4180,84373,043,62044,894,0001,995,0002,285,568
9Mar 27, 2024 20:4580,85975,482,42046,898,00002,519,040
10Mar 27, 2024 20:4480,90271,407,85944,927,0001,996,0002,510,848
11Mar 27, 2024 20:4580,95072,381,19344,954,0001,997,0002,269,184
12Mar 27, 2024 20:4580,96473,550,27645,960,000999,0002,351,104
13Mar 27, 2024 20:4180,98369,087,21145,971,000999,0002,510,848
14Mar 27, 2024 20:4481,02675,781,34444,996,0001,999,0002,285,568
15Mar 27, 2024 20:4181,03473,110,21646,000,0001,000,0002,510,848
16Mar 27, 2024 20:4581,15569,328,75246,069,0001,001,0002,269,184
17Mar 27, 2024 20:4481,19070,069,24945,087,0002,003,0002,285,568
18Mar 27, 2024 20:4581,25769,352,23445,124,0002,005,0002,510,848
19Mar 27, 2024 20:4581,27470,036,61345,134,0002,005,0002,285,568
20Mar 27, 2024 20:4581,39169,365,18845,199,0002,008,0002,506,752
21Mar 27, 2024 20:4481,45070,510,37945,231,0002,010,0002,285,568
22Mar 27, 2024 20:4181,56772,310,71345,296,0002,013,0002,510,848
23Mar 27, 2024 20:4181,60575,111,02346,324,0001,007,0002,285,568
24Mar 27, 2024 20:4481,74867,988,25046,406,0001,008,0002,367,488
25Mar 27, 2024 20:4582,09773,003,07946,603,0001,013,0002,285,568
26Mar 27, 2024 20:4582,20376,249,62746,664,0001,014,0002,342,912
27Mar 27, 2024 20:4482,55274,537,58945,885,0001,995,0002,285,568
28Mar 27, 2024 20:4183,147121,376,20948,225,44502,220,032
29Mar 27, 2024 20:4483,175114,869,50048,241,38302,236,416
30Mar 27, 2024 20:4583,228107,029,41548,272,42802,220,032
31Mar 27, 2024 20:4183,251112,099,07648,285,60302,232,320
32Mar 27, 2024 20:4483,280114,430,34748,302,38902,220,032
33Mar 27, 2024 20:4583,341109,604,55448,337,59002,228,224
34Mar 27, 2024 20:4583,354107,726,05248,345,42102,228,224
35Mar 27, 2024 20:4483,383108,691,34148,361,92402,228,224
36Mar 27, 2024 20:4183,457113,831,37348,404,93102,224,128