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:52E SequeiraE SequeiraScore: 80,288Success
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:5279,11770,443,63243,893,0001,995,0002,285,568
2Mar 27, 2024 20:5279,27873,531,93043,982,0001,999,0002,277,376
3Mar 27, 2024 20:5279,31469,053,54444,002,0002,000,0002,351,104
4Mar 27, 2024 20:5279,35072,878,57144,022,0002,001,0002,285,568
5Mar 27, 2024 20:5379,49868,568,48244,105,0002,004,0002,510,848
6Mar 27, 2024 20:5379,56467,366,87244,141,0002,006,0002,510,848
7Mar 27, 2024 20:5279,61074,454,28144,167,0002,007,0002,510,848
8Mar 27, 2024 20:5379,72968,968,26644,233,0002,010,0002,285,568
9Mar 27, 2024 20:5279,87274,494,56445,319,0001,007,0002,285,568
10Mar 27, 2024 20:5379,92280,129,41445,348,0001,007,0002,285,568
11Mar 27, 2024 20:5380,22169,167,16846,528,00002,363,392
12Mar 27, 2024 20:5380,23667,417,46846,537,00002,506,752
13Mar 27, 2024 20:5380,28874,478,24645,555,0001,012,0002,285,568
14Mar 27, 2024 20:5280,42971,623,80845,635,0001,014,0002,506,752
15Mar 27, 2024 20:5280,44073,817,84246,655,00002,269,184
16Mar 27, 2024 20:5380,84374,779,55044,894,0001,995,0002,285,568
17Mar 27, 2024 20:5280,94569,209,12845,950,000998,0002,363,392
18Mar 27, 2024 20:5380,95773,224,32944,957,0001,998,0002,285,568
19Mar 27, 2024 20:5284,945118,168,09249,268,00602,232,320
20Mar 27, 2024 20:5285,103112,102,87349,359,87202,236,416
21Mar 27, 2024 20:5285,296106,961,82449,471,84602,228,224
22Mar 27, 2024 20:5385,379111,704,94749,519,84202,236,416
23Mar 27, 2024 20:5385,470114,916,87149,572,33802,232,320
24Mar 27, 2024 20:5385,682101,089,28049,695,80102,236,416