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 15, 2025 15:52E SequeiraE SequeiraScore: 86,017Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 15, 2025 15:5484,34572,552,36646,924,0001,996,0002,289,664
2Feb 15, 2025 15:5384,44171,655,17146,977,0001,999,0002,289,664
3Feb 15, 2025 15:5684,50383,314,53247,012,0002,000,0002,523,136
4Feb 15, 2025 15:5384,92472,034,26447,246,0002,010,0002,482,176
5Feb 15, 2025 15:5484,96677,883,66647,269,0002,011,0002,523,136
6Feb 15, 2025 15:5685,20781,563,70848,412,0001,008,0002,289,664
7Feb 15, 2025 15:5385,93676,734,85547,850,0001,993,0002,273,280
8Feb 15, 2025 15:5385,94576,089,67048,852,000996,0002,269,184
9Feb 15, 2025 15:5285,99880,928,20048,882,000997,0002,281,472
10Feb 15, 2025 15:5386,01776,659,90247,895,0001,995,0002,281,472
11Feb 15, 2025 15:5286,02672,843,92347,900,0001,995,0002,281,472
12Feb 15, 2025 15:5686,23677,691,69648,017,0002,000,0002,277,376
13Feb 15, 2025 15:5486,45375,519,50048,138,0002,005,0002,289,664
14Feb 15, 2025 15:5286,50572,921,31045,156,0005,017,0002,519,040
15Feb 15, 2025 15:5386,80374,252,82248,333,0002,013,0002,277,376
16Feb 15, 2025 15:5287,07677,862,29550,504,00002,387,968
17Feb 15, 2025 15:5287,84073,691,45548,950,0001,997,0002,519,040
18Feb 15, 2025 15:5287,98876,992,65749,032,0002,001,0002,396,160