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 22, 2026 07:18limanjun99limanjun99Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:12Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:16011,668,0751,395,000020,480No answer to stdout. Died or killed?
2Apr 22, 2026 07:1876,66760,265,93343,457,0001,010,0002,330,624
3Apr 22, 2026 07:1877,15063,039,58542,759,0001,988,0002,314,240
4Apr 22, 2026 07:1877,47267,425,85842,937,0001,997,0002,289,664
5Apr 22, 2026 07:1878,38967,026,89144,456,0001,010,0002,560,000
6Apr 22, 2026 07:1878,43265,278,68444,481,0001,010,0002,551,808
7Apr 22, 2026 07:1878,66259,555,83644,611,0001,013,0002,334,720
8Apr 22, 2026 07:1878,86361,644,62743,709,0002,032,0002,306,048
9Apr 22, 2026 07:1880,48168,263,64145,665,0001,014,0002,547,712
10Apr 22, 2026 07:1880,88262,309,02544,916,0001,996,0002,539,520