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 listAug 31, 2026 17:17Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 9, 2026 21:40Tomislav Tunkovic10.00CustomSolution Broken
Sep 5, 2026 01:54Tomislav Tunkovic10.00CustomSolution Held
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 9, 2026 21:46010,716,7901,698,00002,121,728expected "2514000 ", got "2533068 "stderr
2Aug 31, 2026 17:182,6899,623,5041,560,00002,121,728stderr
3Aug 31, 2026 17:182,70110,247,4741,567,00002,121,728stderr
4Aug 31, 2026 17:182,7109,689,6801,572,00002,121,728stderr
5Aug 31, 2026 17:182,72510,311,3871,581,00002,121,728stderr
6Aug 31, 2026 17:182,73410,325,3181,586,00002,121,728stderr
7Aug 31, 2026 17:182,90811,296,6281,687,00002,121,728stderr
8Aug 31, 2026 17:1830,01225,776,93816,307,0001,100,0002,117,632stderr
9Aug 31, 2026 17:1830,41026,632,56816,579,0001,059,0002,117,632stderr
10Aug 31, 2026 17:1830,99826,304,65316,897,0001,082,0002,117,632stderr
11Sep 9, 2026 21:4631,61727,457,80017,190,0001,148,0002,117,632stderr
12Sep 9, 2026 21:4633,33728,513,30618,189,0001,147,0002,117,632stderr