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:05Ian MckibbenIan MckibbenError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:14Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:20030,030,194,91630,019,734,0001,020,00016,384Killed by timeout
2Jan 21, 2025 07:05102,53191,511,66858,461,0001,007,0002,531,328
3Jan 21, 2025 07:05102,61986,353,01559,519,00002,416,640
4Jan 21, 2025 07:05102,72683,836,47758,572,0001,009,0002,416,640
5Jan 21, 2025 07:05130,92295,853,19674,936,000999,0002,531,328
6Jan 21, 2025 07:05131,391100,237,51674,202,0002,005,0002,531,328
7Jan 21, 2025 07:05131,553100,784,68976,301,00002,412,544
8Jan 21, 2025 07:05131,621100,589,45176,340,00002,416,640
9Jan 21, 2025 07:05131,77496,579,44476,429,00002,531,328
10Jan 21, 2025 07:05132,205105,397,94076,679,00002,420,736
11Jan 21, 2025 07:05132,790101,430,26175,018,0002,000,0002,531,328
12Jan 21, 2025 07:05132,809100,743,84375,029,0002,000,0002,531,328
13Jan 21, 2025 07:05132,84097,905,89275,046,0002,001,0002,531,328