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 listApr 30, 2025 13:20etherealetherealError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:03Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:06030,018,948,85229,986,139,00020,010,00036,864Killed by timeout
2Apr 30, 2025 13:20125,597104,251,92770,851,0001,995,0002,420,736
3Apr 30, 2025 13:20125,769103,402,46567,950,0004,996,0002,359,296
4Apr 30, 2025 13:20126,24399,513,99972,218,0001,003,0002,289,664
5Apr 30, 2025 13:20126,745100,808,80067,470,0006,042,0002,510,848
6Apr 30, 2025 13:20126,85297,856,49669,543,0004,031,0002,400,256
7Apr 30, 2025 13:20126,89598,926,24670,575,0003,024,0002,281,472
8Apr 30, 2025 13:20127,090104,721,19367,654,0006,058,0002,375,680
9Apr 30, 2025 13:20127,271104,747,00066,835,0006,982,0002,510,848
10Apr 30, 2025 13:20127,509101,968,76566,960,0006,995,0002,445,312