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 24, 2025 00:49HattonuriHattonuriScore: 32,297Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 24, 2025 00:4930,47448,317,33716,636,0001,039,0002,330,624
2Dec 24, 2025 00:5030,85741,126,15915,909,0001,988,0002,523,136
3Dec 24, 2025 00:4930,96741,075,93015,966,0001,995,0002,326,528
4Dec 24, 2025 00:4931,94333,360,30617,498,0001,029,0002,494,464
5Dec 24, 2025 00:5032,29744,724,01716,651,0002,081,0002,306,048
6Dec 24, 2025 00:4932,68842,438,27816,964,0001,995,0002,478,080
7Dec 24, 2025 00:4932,85946,409,68417,052,0002,006,0002,494,464
8Dec 24, 2025 00:5033,28343,074,15417,272,0002,032,0002,445,312
9Dec 24, 2025 00:4933,36645,817,75718,334,0001,018,0002,482,176