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 20:35Josu San MartinJosu San MartinScore: 38,571Success
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 20:3236,97846,116,82121,447,00002,519,040
2Dec 20, 2025 20:3238,25347,111,44020,170,0002,017,0002,371,584
3Dec 20, 2025 20:3538,35753,370,65120,225,0002,022,0002,437,120
4Dec 20, 2025 20:3238,51443,483,08322,338,00002,428,928
5Dec 20, 2025 20:3238,52151,111,41722,342,00002,392,064
6Dec 20, 2025 20:3238,55944,369,26021,348,0001,016,0002,478,080
7Dec 20, 2025 20:3238,57146,596,01121,355,0001,016,0002,383,872
8Dec 20, 2025 20:3239,16453,823,01321,683,0001,032,0002,441,216
9Dec 20, 2025 20:3539,64549,708,39620,995,0001,999,0002,473,984
10Dec 20, 2025 20:3239,75348,809,12521,052,0002,005,0002,260,992
11Dec 20, 2025 20:3539,80751,787,35221,081,0002,007,0002,396,160
12Dec 20, 2025 20:3241,48351,341,63722,055,0002,005,0002,342,912