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 listApr 20, 2024 14:14E SequeiraE SequeiraScore: 76,517Success
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 20, 2024 14:1475,37968,070,66042,704,0001,016,0002,506,752
2Apr 20, 2024 14:1475,43376,702,41842,734,0001,017,0002,506,752
3Apr 20, 2024 14:1475,44070,706,74643,755,00002,342,912
4Apr 20, 2024 14:1475,65272,137,37541,884,0001,994,0002,285,568
5Apr 20, 2024 14:1475,69070,140,89342,903,000997,0002,510,848
6Apr 20, 2024 14:1476,06670,712,79243,116,0001,002,0002,285,568
7Apr 20, 2024 14:1476,51769,404,03443,372,0001,008,0002,265,088
8Apr 20, 2024 14:1476,60268,226,26044,429,00002,519,040
9Apr 20, 2024 14:1476,65267,423,52543,448,0001,010,0002,260,992
10Apr 20, 2024 14:1479,037117,011,55045,841,36402,228,224
11Apr 20, 2024 14:1479,109104,908,48145,883,48502,236,416
12Apr 20, 2024 14:1479,188103,782,52745,928,94402,232,320