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 18, 2025 20:15KeefeKeefeError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:14Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:25030,030,620,12430,013,518,0005,030,00016,384Killed by timeout
2Apr 18, 2025 20:15109,06091,353,19957,231,0006,024,0002,506,752
3Apr 18, 2025 20:15109,63892,125,69661,572,0002,018,0002,490,368
4Apr 18, 2025 20:15110,00088,377,46259,813,0003,987,0002,416,640
5Apr 18, 2025 20:15110,05390,076,86458,845,0004,986,0002,375,680
6Apr 18, 2025 20:15110,59587,229,05760,136,0004,009,0002,490,368
7Apr 18, 2025 20:15111,81696,719,22161,860,0002,993,0002,502,656
8Apr 18, 2025 20:15111,95787,705,95559,940,0004,995,0002,396,160
9Apr 18, 2025 20:15113,88189,339,84861,048,0005,003,0002,375,680
10Apr 18, 2025 20:15113,96794,155,44062,095,0004,006,0002,400,256