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 21, 2025 07:18Ian MckibbenIan MckibbenScore: 87,047Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 21, 2025 07:1886,02972,510,72548,900,000997,0002,269,184
2Jan 21, 2025 07:1886,13676,565,97048,960,000999,0002,269,184
3Jan 21, 2025 07:1886,15976,925,26348,973,000999,0002,412,544
4Jan 21, 2025 07:1886,43471,337,29349,130,0001,002,0002,531,328
5Jan 21, 2025 07:1886,49572,805,48050,167,00002,416,640
6Jan 21, 2025 07:1886,74083,612,78050,309,00002,531,328
7Jan 21, 2025 07:1887,04773,671,03749,478,0001,009,0002,269,184
8Jan 21, 2025 07:1887,05269,833,55349,481,0001,009,0002,408,448
9Jan 21, 2025 07:1887,12172,210,78250,530,00002,412,544
10Jan 21, 2025 07:18119,51490,783,62667,309,0002,009,0002,416,640
11Jan 21, 2025 07:18119,60994,747,02669,373,00002,531,328
12Jan 21, 2025 07:18120,49096,901,52967,888,0001,996,0002,531,328