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 15, 2026 11:45Tomislav TunkovicTomislav TunkovicError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:56Andrew Epstein10.00CustomSolution Broken
Sep 4, 2026 13:28Victor Mercklé10.00CustomSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:58010,151,981952,000028,672No answer to stdout. Died or killed?stderr
2Aug 15, 2026 11:4526,51023,684,16414,325,0001,051,0002,121,728stderr
3Aug 15, 2026 11:4526,81823,949,64814,414,0001,141,0002,121,728stderr
4Aug 15, 2026 11:4527,07424,045,98114,553,0001,150,0002,121,728stderr
5Aug 15, 2026 11:4527,23124,113,12614,512,0001,282,0002,121,728stderr
6Aug 15, 2026 11:4527,44626,186,25214,777,0001,142,0002,121,728stderr
7Aug 15, 2026 11:4527,60125,083,78014,847,0001,162,0002,121,728stderr
8Aug 15, 2026 11:4527,61525,084,24514,845,0001,172,0002,121,728stderr
9Aug 15, 2026 11:4527,70124,402,07414,903,0001,164,0002,121,728stderr
10Aug 15, 2026 11:4528,03724,587,41615,155,0001,107,0002,121,728stderr