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 listDec 23, 2025 23:30HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:58Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:5802,759,583,5372,751,443,000016,384No answer to stdout. Died or killed?
2Dec 23, 2025 23:2830,13445,676,74017,478,00002,318,336
3Dec 23, 2025 23:2830,35341,001,79017,605,00002,322,432
4Dec 23, 2025 23:3030,51737,020,73217,700,00002,490,368
5Dec 23, 2025 23:2830,87135,929,10916,911,000994,0002,506,752
6Dec 23, 2025 23:2831,29043,769,04717,140,0001,008,0002,330,624
7Dec 23, 2025 23:2831,51739,545,92918,280,00002,306,048
8Dec 23, 2025 23:3031,78841,624,91117,413,0001,024,0002,490,368
9Dec 23, 2025 23:2831,80243,598,34517,421,0001,024,0002,465,792
10Dec 23, 2025 23:3031,97941,461,67418,548,00002,314,240
11Dec 23, 2025 23:2831,98445,306,71717,521,0001,030,0002,490,368
12Dec 23, 2025 23:2832,14549,413,62617,609,0001,035,0002,314,240
13Dec 23, 2025 23:2832,26247,403,77118,712,00002,281,472