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:36Aniruddha DebAniruddha DebScore: 8,902Success
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:358,50922,110,4522,961,0001,974,0002,330,624
2Dec 31, 2025 10:378,52921,551,0823,958,000989,0002,273,280
3Dec 31, 2025 10:378,52921,930,9292,968,0001,979,0002,273,280
4Dec 31, 2025 10:378,54524,223,5012,478,0002,478,0002,273,280
5Dec 31, 2025 10:378,59525,850,9473,988,000997,0002,453,504
6Dec 31, 2025 10:378,61720,517,1471,999,0002,999,0002,535,424
7Dec 31, 2025 10:358,69521,958,5793,026,0002,017,0002,347,008
8Dec 31, 2025 10:358,74720,771,6703,044,0002,029,0002,535,424
9Dec 31, 2025 10:368,86719,884,0043,086,0002,057,0002,535,424
10Dec 31, 2025 10:358,90223,686,3754,131,0001,032,0002,342,912
11Dec 31, 2025 10:359,08623,541,8992,108,0003,162,0002,347,008
12Dec 31, 2025 10:369,11224,314,9834,228,0001,057,0002,535,424
13Dec 31, 2025 10:359,32917,866,0445,411,00002,338,816
14Dec 31, 2025 10:379,36719,704,5713,260,0002,173,0002,342,912
15Dec 31, 2025 10:379,38125,744,1684,353,0001,088,0002,351,104
16Dec 31, 2025 10:379,46626,296,5693,294,0002,196,0002,330,624
17Dec 31, 2025 10:369,73420,163,0324,517,0001,129,0002,428,928
18Dec 31, 2025 10:379,88822,780,2304,588,0001,147,0002,535,424