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 23, 2025 14:52Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:20Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:23010,620,302698,000020,480No answer to stdout. Died or killed?
2Dec 23, 2025 14:5234,07249,670,08018,722,0001,040,0002,416,640
3Dec 23, 2025 14:5134,32837,345,44319,910,00002,428,928
4Dec 23, 2025 14:5034,50242,689,10918,010,0002,001,0002,433,024
5Dec 23, 2025 14:5034,62137,651,72119,076,0001,004,0002,465,792
6Dec 23, 2025 14:5434,83347,691,79618,183,0002,020,0002,428,928
7Dec 23, 2025 14:5334,85741,720,64518,196,0002,021,0002,314,240
8Dec 23, 2025 14:5235,02947,009,26119,302,0001,015,0002,498,560
9Dec 23, 2025 14:5335,22143,481,74519,407,0001,021,0002,400,256
10Dec 23, 2025 14:5135,41445,362,58220,540,00002,486,272
11Dec 23, 2025 14:5235,77641,360,75319,713,0001,037,0002,265,088
12Dec 23, 2025 14:5336,05948,639,03719,919,000995,0002,355,200
13Dec 23, 2025 14:5436,27245,359,64019,035,0002,003,0002,334,720
14Dec 23, 2025 14:5136,29843,301,27919,048,0002,005,0002,379,776
15Dec 23, 2025 14:5236,49142,076,08720,158,0001,007,0002,453,504
16Dec 23, 2025 14:5436,51744,601,06419,163,0002,017,0002,383,872
17Dec 23, 2025 14:5236,51945,729,34019,164,0002,017,0002,281,472
18Dec 23, 2025 14:5236,63143,496,33419,223,0002,023,0002,392,064
19Dec 23, 2025 14:5036,68846,909,75820,266,0001,013,0002,293,760