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 listFeb 17, 2025 11:17Bhavik-MakwanaBhavik-MakwanaScore: 134,198Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 17, 2025 11:19131,031103,161,11372,999,0002,999,0002,478,080
2Feb 17, 2025 11:19131,988100,534,58576,553,00002,469,888
3Feb 17, 2025 11:24132,293109,369,59575,721,0001,009,0002,289,664
4Feb 17, 2025 11:17132,833103,806,06275,042,0002,001,0002,281,472
5Feb 17, 2025 11:17133,124101,940,61974,204,0003,008,0002,428,928
6Feb 17, 2025 11:24133,445103,649,12776,393,0001,005,0002,469,888
7Feb 17, 2025 11:24133,645103,459,95676,508,0001,006,0002,314,240
8Feb 17, 2025 11:17134,198103,489,80676,838,000997,0002,473,984
9Feb 17, 2025 11:17135,974108,145,77676,869,0001,996,0002,478,080
10Feb 17, 2025 11:19136,141105,566,38176,963,0001,999,0002,306,048
11Feb 17, 2025 11:17136,812107,860,28777,343,0002,008,0002,469,888
12Feb 17, 2025 11:17139,814109,485,28679,090,0002,002,0002,289,664
13Feb 17, 2025 11:17140,495105,244,39179,475,0002,012,0002,289,664
14Feb 17, 2025 11:17142,879108,012,03580,874,0001,996,0002,478,080
15Feb 17, 2025 11:17142,900108,280,64279,887,0002,995,0002,289,664