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 18, 2026 02:46Victor MerckléVictor MerckléError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 9, 2026 21:38Tomislav Tunkovic10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 9, 2026 21:43031,467,81120,424,0001,123,0002,117,632expected "2208000 ", got "2231554 "stderr
2Sep 9, 2026 21:43031,233,36120,002,0001,102,0002,117,632expected "1580000 ", got "1601771 "stderr
3Sep 9, 2026 21:43031,870,03320,961,0001,013,0002,117,632expected "2827000 ", got "2856145 "stderr
4Aug 18, 2026 03:203,82211,609,6791,196,0001,021,0002,121,728stderr
5Aug 18, 2026 03:203,85111,753,5161,226,0001,008,0002,121,728stderr
6Aug 18, 2026 03:203,90811,710,8161,256,0001,011,0002,121,728stderr
7Aug 18, 2026 03:203,91711,809,9131,133,0001,139,0002,121,728stderr
8Aug 18, 2026 03:203,92012,029,9851,257,0001,017,0002,121,728stderr
9Aug 18, 2026 03:203,97911,758,1751,216,0001,092,0002,121,728stderr
10Aug 18, 2026 03:203,98412,170,0941,225,0001,086,0002,121,728stderr
11Aug 18, 2026 03:203,98711,822,6371,173,0001,140,0002,121,728stderr
12Aug 18, 2026 03:204,16311,792,9231,200,0001,215,0002,121,728stderr