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 27, 2026 12:48Victor MerckléVictor MerckléScore: 380,755Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 27, 2026 12:48378,662236,816,922216,630,0002,994,0002,498,560stderr
2Apr 27, 2026 12:48378,915235,315,868216,775,0002,996,0002,306,048stderr
3Apr 27, 2026 12:48379,913241,284,172218,347,0002,003,0002,490,368stderr
4Apr 27, 2026 12:48380,137245,427,323217,474,0003,006,0002,498,560stderr
5Apr 27, 2026 12:48380,755240,190,985218,840,0001,998,0002,326,528stderr
6Apr 27, 2026 12:48383,136243,938,803220,218,0002,001,0002,506,752stderr
7Apr 27, 2026 12:48383,710239,899,503220,548,0002,004,0002,322,432stderr
8Apr 27, 2026 12:48383,946236,650,942219,694,0002,995,0002,318,336stderr
9Apr 27, 2026 12:48386,141240,688,285220,963,0002,999,0002,494,464stderr