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 listOct 4, 2026 19:49Karthik ReddyKarthik ReddyError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 4, 2026 19:490196,748,698183,220,0003,046,0002,117,632expected "704479 ", got "runs 75750 53921 39001 28000 20194 14798 10531 7829 5679 4194 12058 | Pdd 0.269 | age dec 86 199 348 545 803 1140 1605 2292 3511 | ins dec 5 17 36 65 110 180 295 480 728 "
2Oct 4, 2026 19:490200,526,678186,642,0003,226,0002,117,632expected "1190304 ", got "runs 74649 53687 38439 27514 20188 14791 10771 7926 5605 4233 12210 | Pdd 0.267 | age dec 85 198 350 548 812 1162 1641 2339 3585 | ins dec 5 17 36 64 107 173 281 466 723 "
3Oct 4, 2026 19:490198,625,423184,318,0003,124,0002,117,632expected "948014 ", got "runs 74112 53384 38766 27800 20397 14585 10760 7839 5775 4138 12234 | Pdd 0.265 | age dec 83 196 347 549 811 1156 1625 2304 3519 | ins dec 5 17 35 64 106 173 285 471 724 "