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 listFeb 12, 2026 23:09Code HintsCode HintsError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 22:02Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 22:02026,062,44916,932,000020,480No answer to stdout. Died or killed?
2Feb 12, 2026 23:0936,07453,703,11918,931,0001,992,0002,539,520
3Feb 12, 2026 23:0936,37143,141,05520,091,0001,004,0002,494,464
4Feb 12, 2026 23:0937,31046,456,91120,610,0001,030,0002,502,656
5Feb 12, 2026 23:0937,71251,292,82820,832,0001,041,0002,420,736
6Feb 12, 2026 23:0938,03849,913,72321,060,0001,002,0002,502,656
7Feb 12, 2026 23:0938,30754,544,28720,199,0002,019,0002,498,560
8Feb 12, 2026 23:0938,55247,205,51822,360,00002,486,272
9Feb 12, 2026 23:0939,49849,256,21421,913,000996,0002,424,832
10Feb 12, 2026 23:0939,82249,095,10421,089,0002,008,0002,314,240