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 22, 2025 01:56Josu San MartinJosu San MartinScore: 52,440Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 22, 2025 01:5850,96652,166,16129,560,00002,420,736
2Dec 22, 2025 01:5651,60555,965,08729,931,00002,293,760
3Dec 22, 2025 01:5851,74159,623,01127,009,0003,001,0002,453,504
4Dec 22, 2025 01:5651,75355,416,90528,016,0002,001,0002,363,392
5Dec 22, 2025 01:5651,83853,986,00228,062,0002,004,0002,387,968
6Dec 22, 2025 01:5851,90753,608,24728,099,0002,007,0002,473,984
7Dec 22, 2025 01:5652,38153,845,72030,381,00002,424,832
8Dec 22, 2025 01:5652,44062,960,52630,415,00002,265,088
9Dec 22, 2025 01:5652,44852,837,88729,406,0001,014,0002,383,872
10Dec 22, 2025 01:5652,55359,404,67830,481,00002,445,312
11Dec 22, 2025 01:5652,63352,555,41329,510,0001,017,0002,326,528
12Dec 22, 2025 01:5652,72255,086,07230,579,00002,265,088
13Dec 22, 2025 01:5652,83151,743,00130,642,00002,510,848
14Dec 22, 2025 01:5652,89857,442,30530,681,00002,453,504
15Dec 22, 2025 01:5653,45256,865,87329,002,0002,000,0002,478,080