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 8, 2026 08:47Ashish AhujaAshish AhujaError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:28Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:34011,684,8861,409,000028,672No answer to stdout. Died or killed?stderr
2May 8, 2026 08:48661,274405,483,459382,369,0001,170,0002,482,176stderr
3May 8, 2026 08:48661,624400,661,236382,667,0001,075,0002,465,792stderr
4May 8, 2026 08:48664,862404,408,064384,603,0001,017,0002,465,792stderr
5May 8, 2026 08:48665,734404,409,518384,113,0002,013,0002,473,984stderr
6May 8, 2026 08:48665,977404,335,856385,220,0001,047,0002,482,176stderr
7May 8, 2026 08:48668,982406,670,535386,950,0001,060,0002,453,504stderr
8May 8, 2026 08:48672,587409,410,012389,084,0001,017,0002,461,696stderr
9May 8, 2026 08:48677,456411,372,124391,786,0001,139,0002,478,080stderr
10May 8, 2026 08:48684,512417,494,990395,867,0001,150,0002,469,888stderr