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 1, 2025 12:33Code HintsCode HintsScore: 92,662Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 1, 2025 12:4791,62179,514,55952,138,0001,002,0002,273,280
2Feb 1, 2025 12:4691,72875,725,94552,199,0001,003,0002,514,944
3Feb 1, 2025 12:4191,73678,444,52952,204,0001,003,0002,416,640
4Feb 1, 2025 12:4791,76774,159,22552,221,0001,004,0002,416,640
5Feb 1, 2025 12:4692,11080,484,32553,424,00002,535,424
6Feb 1, 2025 12:4892,12183,058,50852,422,0001,008,0002,535,424
7Feb 1, 2025 12:3392,22675,065,87152,482,0001,009,0002,535,424
8Feb 1, 2025 12:3392,32274,737,41952,537,0001,010,0002,273,280
9Feb 1, 2025 12:4692,43480,344,74452,601,0001,011,0002,416,640
10Feb 1, 2025 12:4792,49578,364,48052,635,0001,012,0002,535,424
11Feb 1, 2025 12:4692,66278,711,46252,730,0001,014,0002,420,736
12Feb 1, 2025 12:3393,17275,335,20552,039,0002,001,0002,420,736
13Feb 1, 2025 12:4193,41778,337,86152,176,0002,006,0002,420,736
14Feb 1, 2025 12:4693,47176,304,88352,206,0002,007,0002,535,424
15Feb 1, 2025 12:4893,72878,816,94054,362,00002,273,280
16Feb 1, 2025 12:4694,08377,953,33853,558,0001,010,0002,416,640
17Feb 1, 2025 12:4894,11276,510,40054,585,00002,424,832
18Feb 1, 2025 12:4194,27480,723,22753,667,0001,012,0002,420,736
19Feb 1, 2025 12:4694,67883,912,77652,917,0001,996,0002,510,848
20Feb 1, 2025 12:4694,78176,401,43553,974,000999,0002,416,640
21Feb 1, 2025 12:4694,86976,143,39454,024,0001,000,0002,514,944