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 listOct 16, 2024 14:36@drytecc@dryteccError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 16, 2024 14:3700000Error: expected "2598528 ", got "2707272 "
2Oct 16, 2024 14:3700000Error: expected "946890 ", got "946770 "
3Oct 16, 2024 14:3700000Error: expected "648836 ", got "649378 "
4Oct 16, 2024 14:3700000Error: expected "1083206 ", got "1421324 "
5Oct 16, 2024 14:3700000Error: expected "723333 ", got "724725 "
6Oct 16, 2024 14:3700000Error: expected "1872594 ", got "1879551 "
7Oct 16, 2024 14:3700000Error: No answer to stdout. Died or killed?
8Oct 16, 2024 14:3700000Error: expected "709682 ", got "795442 "
9Oct 16, 2024 14:3700000Error: expected "2001041 ", got "2001197 "
10Oct 16, 2024 14:3700000Error: expected "1335953 ", got "1373034 "
11Oct 16, 2024 14:3600000Error: expected "856914 ", got "855741 "
12Oct 16, 2024 14:3745,38147,633,31423,284,0003,037,0002,314,240
13Oct 16, 2024 14:3745,73153,455,33124,484,0002,040,0002,310,144
14Oct 16, 2024 14:3745,79750,822,73025,541,0001,021,0002,355,200
15Oct 16, 2024 14:3745,94157,873,04924,597,0002,049,0002,400,256
16Oct 16, 2024 14:3746,01256,562,43926,687,00002,469,888
17Oct 16, 2024 14:3746,05351,115,03624,657,0002,054,0002,478,080