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 GarciaScore: 89,498Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 5, 2025 02:3786,96977,801,49947,416,0003,026,0002,326,528
2Aug 5, 2025 02:3787,42674,562,80747,665,0003,042,0002,465,792
3Aug 5, 2025 02:3787,87874,220,80045,972,0004,997,0002,510,848
4Aug 5, 2025 02:3788,96777,853,40848,566,0003,035,0002,314,240
5Aug 5, 2025 02:3789,49877,367,18347,916,0003,993,0002,355,200
6Aug 5, 2025 02:3789,95974,572,81448,163,0004,013,0002,461,696
7Aug 5, 2025 02:3790,04575,432,18448,209,0004,017,0002,347,008
8Aug 5, 2025 02:3790,14774,649,81547,258,0005,027,0002,445,312
9Aug 5, 2025 02:3790,69779,126,57947,546,0005,058,0002,379,776