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 listDec 18, 2025 14:37Wookyung JeongWookyung JeongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 16:31Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 16:41011,512,3371,383,000020,480No answer to stdout. Died or killed?
2Dec 18, 2025 14:5138,86649,631,65422,542,00002,433,024
3Dec 18, 2025 14:3739,02647,689,46422,635,00002,265,088
4Dec 18, 2025 14:3739,07248,390,05222,662,00002,265,088
5Dec 18, 2025 14:3739,54746,340,69821,940,000997,0002,449,408
6Dec 18, 2025 14:3739,74050,096,07321,045,0002,004,0002,519,040
7Dec 18, 2025 14:3739,74353,251,75021,047,0002,004,0002,506,752
8Dec 18, 2025 14:5139,77149,045,59521,062,0002,005,0002,482,176
9Dec 18, 2025 14:3739,84044,054,71722,103,0001,004,0002,375,680
10Dec 18, 2025 14:3739,84144,803,71821,099,0002,009,0002,338,816
11Dec 18, 2025 14:3739,86449,924,68621,111,0002,010,0002,445,312
12Dec 18, 2025 14:5139,95049,204,42821,157,0002,014,0002,371,584
13Dec 18, 2025 14:3740,02942,510,85420,189,0003,028,0002,269,184