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 08:28limanjun99limanjun99Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:00Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:07011,682,2151,405,000020,480No answer to stdout. Died or killed?
2Apr 23, 2026 08:2848,34345,636,50626,037,0002,002,0002,375,680
3Apr 23, 2026 08:2850,02046,609,93829,012,00002,367,488
4Apr 23, 2026 08:2850,59446,233,97828,334,0001,011,0002,301,952
5Apr 23, 2026 08:2850,81047,961,49228,454,0001,016,0002,416,640
6Apr 23, 2026 08:2851,25648,201,03028,704,0001,025,0002,375,680
7Apr 23, 2026 08:2853,04646,450,81629,742,0001,025,0002,347,008
8Apr 23, 2026 08:2854,38952,628,49930,529,0001,017,0002,367,488
9Apr 23, 2026 08:2855,54449,902,83630,203,0002,013,0002,318,336
10Apr 23, 2026 08:2860,46853,635,18233,068,0002,004,0002,318,336