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 14, 2026 23:42Tomislav 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:5809,034,198662,000020,480No answer to stdout. Died or killed?
2Aug 14, 2026 23:4225,18222,986,17113,538,0001,068,0002,117,632
3Aug 14, 2026 23:4225,71723,213,45713,859,0001,057,0002,117,632
4Aug 14, 2026 23:4225,74323,270,39413,839,0001,092,0002,117,632
5Aug 14, 2026 23:4225,89623,340,50613,918,0001,102,0002,117,632
6Aug 14, 2026 23:4226,33423,621,46914,162,0001,112,0002,117,632
7Aug 14, 2026 23:4226,53123,814,92114,198,0001,190,0002,117,632
8Aug 14, 2026 23:4226,64823,835,11714,359,0001,097,0002,117,632
9Aug 14, 2026 23:4226,71525,071,55314,445,0001,050,0002,117,632
10Aug 14, 2026 23:4226,73423,836,02214,392,0001,114,0002,117,632