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 01:32Josu San MartinJosu San MartinScore: 97,572Success
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 01:3295,83882,891,06450,533,0005,053,0002,351,104
2Dec 19, 2025 01:3296,34082,892,20748,893,0006,984,0002,301,952
3Dec 19, 2025 01:3796,34881,540,80749,895,0005,987,0002,306,048
4Dec 19, 2025 01:3796,47284,595,56550,959,0004,995,0002,326,528
5Dec 19, 2025 01:3296,52182,853,38251,984,0003,998,0002,519,040
6Dec 19, 2025 01:3296,99190,596,06951,233,0005,022,0002,400,256
7Dec 19, 2025 01:3797,15382,103,73949,306,0007,043,0002,371,584
8Dec 19, 2025 01:3797,27279,025,10951,381,0005,037,0002,387,968
9Dec 19, 2025 01:3797,50780,947,63850,495,0006,059,0002,510,848
10Dec 19, 2025 01:3297,57281,163,14351,540,0005,052,0002,383,872
11Dec 19, 2025 01:3297,59383,862,60650,540,0006,064,0002,506,752
12Dec 19, 2025 01:3797,61083,388,60549,538,0007,076,0002,519,040
13Dec 19, 2025 01:3297,62981,762,26849,547,0007,078,0002,351,104
14Dec 19, 2025 01:3797,70587,778,31148,574,0008,095,0002,473,984
15Dec 19, 2025 01:3297,76088,060,11750,626,0006,075,0002,445,312
16Dec 19, 2025 01:3297,92984,964,01950,821,0005,978,0002,289,664
17Dec 19, 2025 01:3798,04888,127,41351,880,0004,988,0002,351,104
18Dec 19, 2025 01:3798,04884,375,72650,882,0005,986,0002,351,104