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 listMay 5, 2026 11:30Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:30Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:44019,878,4908,919,000020,480No answer to stdout. Died or killed?
2May 5, 2026 11:314,54319,809,0852,635,00002,396,160
3May 5, 2026 11:314,75319,068,5912,757,00002,367,488
4May 5, 2026 11:314,76220,702,3142,762,00002,412,544
5May 5, 2026 11:316,82023,290,9733,956,00002,334,720
6May 5, 2026 11:3110,91822,296,1966,333,00002,387,968
7May 5, 2026 11:3111,77224,266,3356,828,00002,371,584
8May 5, 2026 11:3111,77423,133,2965,739,0001,090,0002,543,616
9May 5, 2026 11:3120,27730,137,64610,657,0001,104,0002,473,984
10May 5, 2026 11:3120,74427,329,06610,914,0001,118,0002,551,808