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 listJan 19, 2025 02:31Code HintsCode HintsScore: 454,167Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 19, 2025 02:31452,167286,797,159260,256,0002,001,0002,351,104
2Jan 19, 2025 02:31452,305288,709,837260,335,0002,002,0002,351,104
3Jan 19, 2025 02:28452,436288,854,633261,412,0001,001,0002,289,664
4Jan 19, 2025 02:28453,084287,914,655261,790,000999,0002,289,664
5Jan 19, 2025 02:28453,121292,544,011261,811,000999,0002,289,664
6Jan 19, 2025 02:28453,229284,808,061261,874,000999,0002,428,928
7Jan 19, 2025 02:28453,371288,294,984261,956,000999,0002,289,664
8Jan 19, 2025 02:28453,681290,077,472260,134,0003,001,0002,289,664
9Jan 19, 2025 02:28453,940289,123,461261,283,0002,002,0002,437,120
10Jan 19, 2025 02:28454,167284,847,339260,413,0003,004,0002,289,664
11Jan 19, 2025 02:31454,183287,624,960263,426,00002,289,664
12Jan 19, 2025 02:31454,293284,524,948260,485,0003,005,0002,289,664
13Jan 19, 2025 02:31455,257291,029,890263,049,0001,000,0002,289,664
14Jan 19, 2025 02:31455,748286,692,805261,331,0003,003,0002,289,664
15Jan 19, 2025 02:31456,531291,057,767262,790,0001,998,0002,289,664
16Jan 19, 2025 02:31460,912292,370,315266,328,0001,001,0002,289,664
17Jan 19, 2025 02:31479,686303,833,239277,218,0001,000,0002,342,912
18Jan 19, 2025 02:28482,586304,358,245277,901,0001,999,0002,351,104