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 listAug 31, 2026 03:38Tomislav TunkovicTomislav TunkovicError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:56Andrew Epstein10.00CustomSolution Broken
Sep 4, 2026 16:00Victor Mercklé10.00CustomSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:5709,122,239656,000024,576No answer to stdout. Died or killed?stderr
2Aug 31, 2026 03:3922,42221,324,34213,005,000024,576stderr
3Aug 31, 2026 03:3922,60521,379,03413,111,000024,576stderr
4Aug 31, 2026 03:3922,92021,575,61613,294,000024,576stderr
5Aug 31, 2026 03:3922,95821,609,77513,316,000024,576stderr
6Aug 31, 2026 03:3923,47721,994,16713,617,000024,576stderr
7Aug 31, 2026 03:3923,76822,023,36313,786,000024,576stderr
8Aug 31, 2026 03:3923,95022,193,26013,891,000024,576stderr
9Aug 31, 2026 03:3924,18722,245,58214,029,000024,576stderr
10Aug 31, 2026 03:3924,58923,723,57314,262,000024,576stderr