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 29, 2025 22:05Aniruddha DebAniruddha DebError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:26Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:26011,019,3323,297,00002,117,632expected "999999000 ", got "0 "
2Jul 5, 2026 20:2609,776,5341,933,00002,117,632expected "999999000 ", got "0 "
3Jul 5, 2026 20:26010,823,4283,210,00002,117,632expected "999999000 ", got "0 "
4Dec 29, 2025 22:0610,32119,665,2134,989,000997,0002,523,136
5Dec 29, 2025 22:0610,61622,600,9814,105,0002,052,0002,461,696
6Dec 29, 2025 22:0611,93622,574,3465,934,000989,0002,478,080
7Dec 29, 2025 22:0612,24721,037,5436,089,0001,014,0002,289,664
8Dec 29, 2025 22:0612,95223,221,5537,512,00002,523,136
9Dec 29, 2025 22:0613,24725,394,1217,683,00002,461,696
10Dec 29, 2025 22:0615,02224,164,8938,713,00002,453,504
11Dec 29, 2025 22:0615,69026,016,7887,078,0002,022,0002,523,136
12Dec 29, 2025 22:0615,80726,435,3868,150,0001,018,0002,449,408
13Dec 29, 2025 22:0525,70038,445,07814,906,00002,502,656
14Dec 29, 2025 22:0626,37426,678,98815,297,00002,498,560
15Dec 29, 2025 22:0627,37640,194,29514,886,000992,0002,449,408
16Dec 29, 2025 22:0629,89349,394,51617,338,00002,494,464
17Dec 29, 2025 22:0534,50348,152,81019,012,0001,000,0002,494,464
18Dec 29, 2025 22:0536,14043,152,07919,963,000998,0002,498,560