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 listJan 30, 2026 16:59olliecrowolliecrowError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 5, 2026 05:52Tomislav Tunkovic54.47CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 5, 2026 06:16015,906,4393,907,0001,420,0002,121,728expected "200492606 ", got "0 "
2Sep 5, 2026 06:16015,971,6644,167,0001,241,0002,121,728expected "4718941 ", got "648287 "
3Sep 5, 2026 06:16014,832,2673,754,0001,185,0002,121,728expected "404952061 ", got "34450 "
4Jan 30, 2026 16:5913,89325,376,5906,044,0002,014,0002,502,656
5Jan 30, 2026 16:5913,98323,593,3786,083,0002,027,0002,490,368
6Jan 30, 2026 16:5914,01625,265,0866,097,0002,032,0002,281,472
7Jan 30, 2026 16:5914,22825,663,7566,189,0002,063,0002,277,376
8Jan 30, 2026 16:5914,41925,692,3237,318,0001,045,0002,281,472
9Jan 30, 2026 16:5914,75726,346,2118,559,00002,531,328
10Jan 30, 2026 16:5914,86625,865,9637,545,0001,077,0002,285,568
11Jan 30, 2026 16:5914,97125,048,1177,598,0001,085,0002,301,952
12Jan 30, 2026 16:5915,35724,637,9846,928,0001,979,0002,469,888