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 listDec 22, 2025 03:06Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:26Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:36010,743,396785,000020,480No answer to stdout. Died or killed?
2Dec 22, 2025 03:0634,96242,465,45020,278,00002,519,040
3Dec 22, 2025 03:0635,62651,211,67119,630,0001,033,0002,510,848
4Dec 22, 2025 03:0635,81953,617,78119,737,0001,038,0002,482,176
5Dec 22, 2025 03:0636,06250,425,42718,924,0001,992,0002,420,736
6Dec 22, 2025 03:0636,06647,941,12218,926,0001,992,0002,478,080
7Dec 22, 2025 03:0636,22241,202,49519,009,0002,000,0002,506,752
8Dec 22, 2025 03:0636,33649,333,43919,068,0002,007,0002,441,216
9Dec 22, 2025 03:0636,54340,270,32519,177,0002,018,0002,465,792
10Dec 22, 2025 03:0636,70942,222,17020,278,0001,013,0002,502,656
11Dec 22, 2025 03:0636,73645,879,79320,293,0001,014,0002,379,776
12Dec 22, 2025 03:0636,97843,518,01021,447,00002,310,144
13Dec 22, 2025 03:0636,98443,301,69820,430,0001,021,0002,392,064
14Dec 22, 2025 03:0637,00345,540,12220,440,0001,022,0002,330,624
15Dec 22, 2025 03:0637,07444,046,73421,503,00002,437,120
16Dec 22, 2025 03:0637,07644,726,95320,480,0001,024,0002,428,928
17Dec 22, 2025 03:0637,07847,245,96021,505,00002,437,120
18Dec 22, 2025 03:0637,44851,196,74020,686,0001,034,0002,400,256
19Dec 22, 2025 03:0637,75341,714,67320,902,000995,0002,498,560