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 listMar 30, 2025 08:07Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 30, 2025 08:0800000Error: No answer to stdout. Died or killed?stderr
2Mar 30, 2025 08:0700000Error: No answer to stdout. Died or killed?stderr
3Mar 30, 2025 07:4900000Error: No answer to stdout. Died or killed?stderr
4Mar 30, 2025 07:4800000Error: No answer to stdout. Died or killed?stderr
5Mar 30, 2025 07:4700000Error: No answer to stdout. Died or killed?stderr
6Mar 30, 2025 07:4700000Error: No answer to stdout. Died or killed?stderr
7Mar 30, 2025 07:46144,269115,132,99582,668,0001,008,0002,285,568
8Mar 30, 2025 07:46144,553109,223,41282,843,000998,0002,498,560
9Mar 30, 2025 07:48144,557116,188,95281,847,0001,996,0002,285,568
10Mar 30, 2025 07:46144,562109,543,24381,850,0001,996,0002,502,656
11Mar 30, 2025 07:48144,628110,928,99182,874,0001,010,0002,482,176
12Mar 30, 2025 07:48144,652106,595,87481,901,0001,997,0002,285,568
13Mar 30, 2025 07:49145,186106,386,76983,206,0001,002,0002,347,008
14Mar 30, 2025 07:49145,191105,563,33382,206,0002,005,0002,506,752
15Mar 30, 2025 08:08145,412107,930,66283,335,0001,004,0002,285,568
16Mar 30, 2025 08:08145,529110,848,76483,403,0001,004,0002,285,568
17Mar 30, 2025 07:49145,655106,975,22983,475,0001,005,0002,285,568
18Mar 30, 2025 08:08146,000113,150,67683,684,000996,0002,285,568
19Mar 30, 2025 07:50146,921107,913,92883,209,0002,005,0002,486,272
20Mar 30, 2025 07:50146,933107,833,67983,216,0002,005,0002,490,368
21Mar 30, 2025 08:07147,031107,438,15484,275,0001,003,0002,285,568
22Mar 30, 2025 07:50147,152112,508,46983,340,0002,008,0002,478,080
23Mar 30, 2025 08:07147,666111,352,85283,655,0001,991,0002,453,504
24Mar 30, 2025 08:07148,147108,495,61384,926,000999,0002,301,952