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 listDec 31, 2025 16:47Aniruddha DebAniruddha DebScore: 8,500Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 31, 2025 16:478,47620,950,3622,950,0001,966,0002,424,832
2Dec 31, 2025 16:478,47926,181,9963,935,000983,0002,322,432
3Dec 31, 2025 16:478,47925,594,1123,935,000983,0002,527,232
4Dec 31, 2025 16:478,49724,037,2264,928,00002,273,280
5Dec 31, 2025 16:478,50025,604,8283,944,000986,0002,265,088
6Dec 31, 2025 16:478,50526,603,0583,700,0001,233,0002,330,624
7Dec 31, 2025 16:478,69519,450,6803,026,0002,017,0002,334,720
8Dec 31, 2025 16:478,85023,824,8753,080,0002,053,0002,531,328
9Dec 31, 2025 16:478,95320,009,9243,116,0002,077,0002,412,544