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 23, 2026 05:17limanjun99limanjun99Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:17Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:28011,476,8121,367,000020,480No answer to stdout. Died or killed?
2Apr 23, 2026 05:1752,56549,192,60130,488,00002,375,680
3Apr 23, 2026 05:1754,00047,744,19930,310,0001,010,0002,367,488
4Apr 23, 2026 05:1754,71247,127,34230,710,0001,023,0002,301,952
5Apr 23, 2026 05:1754,81047,493,78629,804,0001,986,0002,355,200
6Apr 23, 2026 05:1755,31850,994,61830,080,0002,005,0002,371,584
7Apr 23, 2026 05:1755,91254,928,80631,416,0001,013,0002,355,200
8Apr 23, 2026 05:1758,11546,958,97831,665,0002,042,0002,351,104
9Apr 23, 2026 05:1759,36250,159,82533,418,0001,012,0002,375,680
10Apr 23, 2026 05:1759,60352,129,07133,554,0001,016,0002,342,912