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 21, 2025 01:57HattonuriHattonuriScore: 35,816Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 01:5633,09843,734,53917,177,0002,020,0002,428,928
2Dec 21, 2025 01:5633,37840,448,71819,359,00002,531,328
3Dec 21, 2025 01:5735,21748,786,13719,405,0001,021,0002,523,136
4Dec 21, 2025 01:5635,22439,328,49720,430,00002,531,328
5Dec 21, 2025 01:5635,25040,702,33520,445,00002,420,736
6Dec 21, 2025 01:5635,63145,792,97919,633,0001,033,0002,523,136
7Dec 21, 2025 01:5635,81649,163,12219,735,0001,038,0002,523,136
8Dec 21, 2025 01:5635,98645,617,66919,879,000993,0002,523,136
9Dec 21, 2025 01:5636,03145,082,93719,903,000995,0002,523,136
10Dec 21, 2025 01:5736,21644,016,01520,005,0001,000,0002,523,136
11Dec 21, 2025 01:5636,34545,673,18719,073,0002,007,0002,416,640
12Dec 21, 2025 01:5736,42848,195,35319,116,0002,012,0002,420,736