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 15:23Daniel HarrisDaniel HarrisScore: 116,019Success
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 15:2386,94079,026,37949,417,0001,008,0002,420,736
2Feb 7, 2025 15:2391,51678,295,11352,078,0001,001,0002,424,832
3Feb 7, 2025 15:2392,24875,637,80352,495,0001,009,0002,392,064
4Feb 7, 2025 15:2393,87980,704,13954,450,00002,363,392
5Feb 7, 2025 15:2395,57186,257,48154,424,0001,007,0002,375,680
6Feb 7, 2025 15:2395,67483,848,03554,483,0001,008,0002,371,584
7Feb 7, 2025 15:2397,71089,408,80356,672,00002,347,008
8Feb 7, 2025 15:23101,04586,294,75958,606,00002,490,368
9Feb 7, 2025 15:23105,47889,657,94861,177,00002,445,312
10Feb 7, 2025 15:23116,01992,289,70165,283,0002,008,0002,490,368
11Feb 7, 2025 15:23117,01793,974,34566,872,000998,0002,473,984
12Feb 7, 2025 15:23117,55793,800,30667,181,0001,002,0002,482,176
13Feb 7, 2025 15:23119,245100,247,76268,160,0001,002,0002,486,272
14Feb 7, 2025 15:23120,636101,720,83568,970,000999,0002,379,776
15Feb 7, 2025 15:23122,24095,310,89668,902,0001,997,0002,469,888
16Feb 7, 2025 15:23128,06295,308,00072,269,0002,007,0002,363,392
17Feb 7, 2025 15:23129,336105,838,55873,015,0002,000,0002,355,200
18Feb 7, 2025 15:23131,571106,394,02474,303,0002,008,0002,289,664