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 29, 2023 03:16yenw0dyenw0dScore: 146,750Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 29, 2023 03:16135,638107,546,14376,653,0002,017,0002,322,432
2Nov 29, 2023 03:16137,707113,066,47876,875,0002,995,0002,531,328
3Nov 29, 2023 03:16138,116107,696,23679,106,0001,001,0002,338,816
4Nov 29, 2023 03:16139,469109,393,38476,898,0003,994,0002,342,912
5Nov 29, 2023 03:16145,169118,245,70282,194,0002,004,0002,478,080
6Nov 29, 2023 03:16145,481108,484,64381,366,0003,013,0002,441,216
7Nov 29, 2023 03:16146,750112,872,16782,111,0003,004,0002,531,328
8Nov 29, 2023 03:16153,322118,103,41486,929,0001,998,0002,383,872
9Nov 29, 2023 03:16156,643122,871,03787,858,0002,995,0002,314,240
10Nov 29, 2023 03:16163,909176,217,61495,067,34302,306,048
11Nov 29, 2023 03:16163,970150,826,75095,102,73702,199,552
12Nov 29, 2023 03:16164,128162,444,83995,194,19502,260,992