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 7, 2025 12:37Daniel HarrisDaniel HarrisScore: 124,448Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 7, 2025 12:4391,18880,386,94951,892,000997,0002,428,928
2Feb 7, 2025 12:3892,53385,872,08252,657,0001,012,0002,412,544
3Feb 7, 2025 12:4393,38677,813,97552,158,0002,006,0002,433,024
4Feb 7, 2025 12:3798,28481,469,76756,005,0001,000,0002,383,872
5Feb 7, 2025 12:38102,03884,731,27257,176,0002,006,0002,428,928
6Feb 7, 2025 12:43102,23480,818,45157,286,0002,010,0002,387,968
7Feb 7, 2025 12:37103,26086,297,10557,895,0001,996,0002,486,272
8Feb 7, 2025 12:38103,40588,259,93758,976,000999,0002,469,888
9Feb 7, 2025 12:37105,87289,308,41861,406,00002,420,736
10Feb 7, 2025 12:37120,93492,327,98269,140,0001,002,0002,478,080
11Feb 7, 2025 12:37122,94195,397,04769,298,0002,008,0002,367,488
12Feb 7, 2025 12:43124,24896,624,16571,064,0001,000,0002,486,272
13Feb 7, 2025 12:37124,44897,999,88170,175,0002,005,0002,383,872
14Feb 7, 2025 12:38124,87292,171,45771,421,0001,005,0002,482,176
15Feb 7, 2025 12:37124,99798,151,24570,485,0002,013,0002,387,968
16Feb 7, 2025 12:37125,63497,424,62371,870,000998,0002,383,872
17Feb 7, 2025 12:43125,681102,604,49771,897,000998,0002,469,888
18Feb 7, 2025 12:37128,13696,428,55372,311,0002,008,0002,461,696
19Feb 7, 2025 12:37128,54199,939,19073,547,0001,007,0002,363,392
20Feb 7, 2025 12:38128,58394,010,12173,571,0001,007,0002,326,528
21Feb 7, 2025 12:43133,30798,589,02376,314,0001,004,0002,355,200
22Feb 7, 2025 12:37136,552105,464,57078,198,0001,002,0002,445,312
23Feb 7, 2025 12:38142,017106,531,47681,366,0001,004,0002,318,336
24Feb 7, 2025 12:37143,436107,980,81082,191,0001,002,0002,322,432