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 5, 2025 02:37Drew GarciaDrew GarciaError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:29Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:32029,822,77816,716,0001,018,000110,592No answer to stdout. Died or killed?stderr
2Aug 5, 2025 02:3786,96977,801,49947,416,0003,026,0002,326,528
3Aug 5, 2025 02:3787,42674,562,80747,665,0003,042,0002,465,792
4Aug 5, 2025 02:3787,87874,220,80045,972,0004,997,0002,510,848
5Aug 5, 2025 02:3788,96777,853,40848,566,0003,035,0002,314,240
6Aug 5, 2025 02:3789,49877,367,18347,916,0003,993,0002,355,200
7Aug 5, 2025 02:3789,95974,572,81448,163,0004,013,0002,461,696
8Aug 5, 2025 02:3790,04575,432,18448,209,0004,017,0002,347,008
9Aug 5, 2025 02:3790,14774,649,81547,258,0005,027,0002,445,312
10Aug 5, 2025 02:3790,69779,126,57947,546,0005,058,0002,379,776