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

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

Challenge History
DateChallengerCostTypeStatus
Sep 4, 2026 12:08Tomislav Tunkovic10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 4, 2026 12:09011,665,75401,757,00036,864No answer to stdout. Died or killed?
2May 5, 2026 14:427,05512,058,9092,095,0001,997,0002,564,096
3May 5, 2026 14:427,21712,084,4462,313,0001,873,0002,527,232
4May 5, 2026 14:427,26012,118,7612,367,0001,844,0002,523,136
5May 5, 2026 14:427,30312,093,1842,234,0002,002,0002,576,384
6May 5, 2026 14:427,33612,109,3972,868,0001,387,0002,564,096
7May 5, 2026 14:427,46012,166,3032,909,0001,418,0002,506,752
8May 5, 2026 14:427,66812,309,9753,005,0001,443,0002,551,808
9May 5, 2026 14:427,70012,201,5822,462,0002,004,0002,527,232
10May 5, 2026 14:427,70334,697,4393,036,0001,432,0002,527,232