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 5, 2023 01:03apuxbtapuxbtScore: 84,053Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2023 01:0382,659110,148,11947,942,29502,195,456
2Oct 5, 2023 01:0382,81972,232,92341,030,0007,005,0002,371,584
3Oct 5, 2023 01:0382,97670,903,50244,116,0004,010,0002,490,368
4Oct 5, 2023 01:0383,22470,144,77743,242,0005,028,0002,461,696
5Oct 5, 2023 01:0383,64768,765,51343,462,0005,053,0002,293,760
6Oct 5, 2023 01:0383,68372,593,39642,469,0006,067,0002,478,080
7Oct 5, 2023 01:0384,05373,553,88941,787,0006,964,0002,404,352
8Oct 5, 2023 01:0384,168113,971,36848,817,34602,195,456
9Oct 5, 2023 01:0384,56073,088,10844,041,0005,004,0002,482,176
10Oct 5, 2023 01:0384,87276,309,59944,203,0005,023,0002,490,368
11Oct 5, 2023 01:0385,16472,299,31742,339,0007,056,0002,367,488
12Oct 5, 2023 01:0387,188107,148,66650,568,95602,195,456