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 listDec 26, 2024 13:38Louis PonetLouis PonetError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2024 13:3800000Error: expected "708988 ", got "706247"
2Dec 25, 2024 22:261,388,590831,586,765798,388,0006,994,0002,273,280
3Dec 25, 2024 22:261,388,681834,474,682800,439,0004,996,0002,519,040
4Dec 25, 2024 22:261,390,571837,915,690801,534,0004,997,0002,490,368
5Dec 26, 2024 13:381,391,993837,046,676802,360,0004,996,0002,269,184
6Dec 25, 2024 22:261,394,090835,758,241803,575,0004,997,0002,289,664
7Dec 25, 2024 22:261,396,567837,025,008806,009,0004,000,0002,318,336
8Dec 26, 2024 13:381,397,272836,319,653805,422,0004,996,0002,285,568
9Dec 25, 2024 22:261,399,234838,163,501806,559,0004,997,0002,404,352
10Dec 25, 2024 22:261,400,683840,626,681807,400,0004,996,0002,469,888
11Dec 25, 2024 22:261,400,748841,506,322807,438,0004,996,0002,482,176
12Dec 26, 2024 13:381,401,545842,327,168808,897,0003,999,0002,322,432
13Dec 25, 2024 22:261,404,905845,034,361809,846,0004,999,0002,338,816