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 12:12Tomislav 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 16:00Victor Mercklé10.00CustomSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:5809,132,853652,000020,480No answer to stdout. Died or killed?
2Aug 15, 2026 12:1223,63622,782,43913,709,000020,480
3Aug 15, 2026 12:1224,00822,228,45213,925,000020,480
4Aug 15, 2026 12:1224,12722,410,87513,994,000020,480
5Aug 15, 2026 12:1224,18722,351,04414,029,000020,480
6Aug 15, 2026 12:1224,21022,333,10014,042,000020,480
7Aug 15, 2026 12:1224,42022,491,64414,164,000020,480
8Aug 15, 2026 12:1224,67522,718,43614,312,000020,480
9Aug 15, 2026 12:1224,77922,676,01814,372,000020,480
10Aug 15, 2026 12:1225,48123,075,23014,779,000020,480