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 31, 2025 10:35Aniruddha DebAniruddha DebScore: 9,066Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 31, 2025 10:348,55223,344,5492,976,0001,984,0002,535,424
2Dec 31, 2025 10:358,57919,765,8802,986,0001,990,0002,424,832
3Dec 31, 2025 10:228,62424,672,2993,001,0002,001,0002,338,816
4Dec 31, 2025 10:228,68624,767,7004,031,0001,007,0002,531,328
5Dec 31, 2025 10:348,71221,906,1553,032,0002,021,0002,342,912
6Dec 31, 2025 10:348,81623,307,5922,045,0003,068,0002,342,912
7Dec 31, 2025 10:228,91925,059,5404,139,0001,034,0002,531,328
8Dec 31, 2025 10:229,06619,308,4734,207,0001,051,0002,535,424
9Dec 31, 2025 10:229,16919,826,1693,191,0002,127,0002,342,912
10Dec 31, 2025 10:349,39021,127,1583,268,0002,178,0002,330,624
11Dec 31, 2025 10:349,42621,768,2213,280,0002,187,0002,334,720
12Dec 31, 2025 10:359,47822,446,3964,398,0001,099,0002,428,928
13Dec 31, 2025 10:229,50522,577,2733,308,0002,205,0002,326,528
14Dec 31, 2025 10:349,52624,242,1352,210,0003,315,0002,342,912
15Dec 31, 2025 10:359,62226,965,5974,465,0001,116,0002,347,008