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 28, 2026 13:58Dominique GarmierDominique GarmierError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 28, 2026 13:58019,743,9223,357,0001,119,0002,318,336expected "2509041 ", got "2512328 "
2Apr 28, 2026 13:587,40321,271,6243,221,0001,073,0002,318,336
3Apr 28, 2026 13:587,45521,692,5203,243,0001,081,0002,576,384
4Apr 28, 2026 13:587,46220,884,0823,246,0001,082,0002,322,432
5Apr 28, 2026 13:587,47520,425,6723,252,0001,084,0002,318,336
6Apr 28, 2026 13:587,50317,498,7333,264,0001,088,0002,334,720
7Apr 28, 2026 13:587,50523,984,6943,265,0001,088,0002,408,448
8Apr 28, 2026 13:587,54426,401,0983,282,0001,094,0002,318,336
9Apr 28, 2026 13:587,68222,801,7703,342,0001,114,0002,318,336