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 21, 2026 16:53Charles CabergsCharles CabergsError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:38Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:43011,664,9461,290,000020,480No answer to stdout. Died or killed?
2Apr 21, 2026 16:5344,48241,548,07523,816,0001,984,0002,420,736
3Apr 21, 2026 16:5345,26743,367,40824,236,0002,019,0002,449,408
4Apr 21, 2026 16:5346,62244,570,29425,038,0002,003,0002,498,560
5Apr 21, 2026 16:5347,14842,376,88925,321,0002,025,0002,473,984
6Apr 21, 2026 16:5347,62950,740,81025,579,0002,046,0002,433,024
7Apr 21, 2026 16:5348,28747,308,03526,007,0002,000,0002,551,808
8Apr 21, 2026 16:5348,35046,286,13224,037,0004,006,0002,400,256
9Apr 21, 2026 16:5348,36044,162,55825,044,0003,005,0002,510,848
10Apr 21, 2026 16:5348,56049,626,41024,142,0004,023,0002,408,448