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 20, 2025 18:58Josu San MartinJosu San MartinScore: 58,584Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 18:5857,56664,652,98432,377,0001,011,0002,273,280
2Dec 20, 2025 18:0457,73157,624,75633,484,00002,273,280
3Dec 20, 2025 18:0458,00758,524,70733,644,00002,506,752
4Dec 20, 2025 18:5858,10965,546,66632,682,0001,021,0002,273,280
5Dec 20, 2025 18:5858,13659,051,80032,698,0001,021,0002,506,752
6Dec 20, 2025 18:0458,42859,292,61731,895,0001,993,0002,273,280
7Dec 20, 2025 18:0458,45758,819,32231,911,0001,994,0002,387,968
8Dec 20, 2025 18:0458,46662,610,79732,913,000997,0002,383,872
9Dec 20, 2025 18:0458,57960,610,00631,978,0001,998,0002,273,280
10Dec 20, 2025 18:0458,58460,618,95432,980,000999,0002,387,968
11Dec 20, 2025 18:5858,65554,294,61032,019,0002,001,0002,273,280
12Dec 20, 2025 18:5858,73855,205,25732,064,0002,004,0002,273,280
13Dec 20, 2025 18:5858,82959,295,94232,114,0002,007,0002,273,280
14Dec 20, 2025 18:5858,90559,175,96533,161,0001,004,0002,281,472
15Dec 20, 2025 18:5858,93156,169,58233,175,0001,005,0002,273,280
16Dec 20, 2025 18:5859,03465,769,41633,233,0001,007,0002,273,280
17Dec 20, 2025 18:0459,13362,661,70033,289,0001,008,0002,392,064
18Dec 20, 2025 18:0459,45960,081,96733,472,0001,014,0002,273,280