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 19, 2025 18:10Josu San MartinJosu San MartinScore: 61,879Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 19, 2025 17:3661,03856,383,58134,391,0001,011,0002,277,376
2Dec 19, 2025 17:3661,25958,185,61135,530,00002,330,624
3Dec 19, 2025 17:3661,41956,794,75934,606,0001,017,0002,334,720
4Dec 19, 2025 17:3661,60062,329,22034,708,0001,020,0002,277,376
5Dec 19, 2025 17:3661,61662,158,80035,737,00002,342,912
6Dec 19, 2025 17:3661,87961,645,48733,897,0001,993,0002,342,912
7Dec 19, 2025 17:3661,87962,016,59233,897,0001,993,0002,334,720
8Dec 19, 2025 18:1061,89063,610,25233,902,0001,994,0002,277,376
9Dec 19, 2025 17:3662,10362,729,22034,019,0002,001,0002,338,816
10Dec 19, 2025 18:1062,10762,310,31635,022,0001,000,0002,277,376
11Dec 19, 2025 17:3662,20961,053,38234,077,0002,004,0002,277,376
12Dec 19, 2025 18:1062,34157,256,25135,154,0001,004,0002,277,376