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 14:05Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 4, 2026 12:04Tomislav Tunkovic10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 4, 2026 12:0509,729,1081,656,000016,384No answer to stdout. Died or killed?
2May 5, 2026 14:124,67016,194,9912,709,00002,306,048
3May 5, 2026 14:124,78215,937,4802,774,00002,347,008
4May 5, 2026 14:125,01215,576,9642,907,00002,490,368
5May 5, 2026 14:125,04318,673,7742,925,00002,478,080
6May 5, 2026 14:125,08717,781,5722,951,00002,318,336
7May 5, 2026 14:125,25318,631,2573,047,00002,514,944
8May 5, 2026 14:125,63430,019,4173,268,00002,412,544
9May 5, 2026 14:1237,81736,771,51021,934,00002,535,424
10May 5, 2026 14:1238,47244,123,53522,314,00002,486,272