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 listNov 22, 2024 01:23Joad NacerJoad NacerScore: 2,457,393Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 22, 2024 01:232,394,0311,416,515,8311,386,539,0001,999,0002,281,472
2Nov 22, 2024 01:212,418,4141,428,306,2501,400,681,0001,999,0002,482,176
3Nov 22, 2024 01:242,432,5411,437,447,9711,405,878,0004,996,0002,281,472
4Nov 22, 2024 01:212,448,1741,447,971,7461,416,944,0002,997,0002,486,272
5Nov 22, 2024 01:242,457,3931,454,703,7561,421,290,0003,998,0002,482,176
6Nov 22, 2024 01:212,457,5571,455,520,7041,423,384,0001,999,0002,281,472
7Nov 22, 2024 01:232,498,7831,475,238,9361,448,295,000999,0002,281,472
8Nov 22, 2024 01:232,760,8141,626,680,5251,600,273,000999,0002,478,080
9Nov 22, 2024 01:242,766,2261,633,438,7651,602,413,0001,998,0002,478,080