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 listNov 22, 2024 15:08Joad NacerJoad NacerError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:18Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:3309,385,452880,000020,480No answer to stdout. Died or killed?
2Nov 22, 2024 15:08121,09792,433,73366,223,0004,013,0002,453,504
3Nov 22, 2024 15:08121,56096,742,50168,491,0002,014,0002,445,312
4Nov 22, 2024 15:08123,09591,110,87869,384,0002,011,0002,306,048
5Nov 22, 2024 15:08123,37998,694,89367,529,0004,031,0002,531,328
6Nov 22, 2024 15:08124,45798,287,18967,173,0005,012,0002,437,120
7Nov 22, 2024 15:08126,536100,505,54670,375,0003,016,0002,359,296
8Nov 22, 2024 15:08126,947103,797,74972,621,0001,008,0002,412,544
9Nov 22, 2024 15:08127,32899,453,55269,859,0003,991,0002,441,216
10Nov 22, 2024 15:08129,612104,496,15170,164,0005,011,0002,441,216