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 listAug 31, 2026 15:15Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 10, 2026 14:43Tomislav Tunkovic52.20CustomSolution Broken
Sep 5, 2026 03:55Tomislav Tunkovic10.00CustomSolution Held
Sep 4, 2026 13:28Tomislav Tunkovic10.00CustomSolution Held
Sep 4, 2026 11:56Tomislav Tunkovic10.00CustomSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 10, 2026 14:44011,308,9632,224,00002,199,552expected "3363538 ", got "3390869 "stderr
2Sep 10, 2026 14:44014,245,41902,349,0002,199,552expected "2333143 ", got "2355431 "stderr
3Aug 31, 2026 15:193,47010,322,7592,013,00002,170,880stderr
4Aug 31, 2026 15:193,48410,223,2682,021,00002,162,688stderr
5Aug 31, 2026 15:193,53610,258,3702,051,00002,162,688stderr
6Aug 31, 2026 15:193,66010,286,2312,123,00002,170,880stderr
7Aug 31, 2026 15:193,69310,338,31402,142,0002,199,552stderr
8Aug 31, 2026 15:193,72010,357,47502,158,0002,170,880stderr
9Aug 31, 2026 15:193,78910,396,86902,198,0002,199,552stderr
10Aug 31, 2026 15:194,00310,552,64002,322,0002,199,552stderr
11Aug 31, 2026 15:1932,55527,330,58017,553,0001,329,0002,142,208stderr
12Sep 10, 2026 14:4434,39630,716,68718,909,0001,041,0002,142,208stderr