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 28, 2025 19:06Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:19Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:20018,798,1156,730,0001,230,00098,304Exit with code 2: unexpected fault address 0x7536a4ac0 fatal error: fault [signal SIGSEGV: segmentation violation code=0x1 addr=0x7536a4ac0 pc=0x473974] goroutine 1 gp=0xc000002380 m=0 mp=0x511ae0 [running]: runtime.throw({0x488e08?, 0x0?}) /compiler...stderr
2Dec 28, 2025 19:0611,91424,318,4585,923,000987,0002,506,752
3Dec 28, 2025 19:0612,09525,641,6095,011,0002,004,0002,519,040
4Dec 28, 2025 19:0612,25022,454,8996,090,0001,015,0002,482,176
5Dec 28, 2025 19:0612,30025,151,7276,115,0001,019,0002,482,176
6Dec 28, 2025 19:0612,32424,866,0075,106,0002,042,0002,453,504
7Dec 28, 2025 19:0612,51224,677,9205,184,0002,073,0002,531,328
8Dec 28, 2025 19:0612,66226,827,6246,295,0001,049,0002,494,464
9Dec 28, 2025 19:0612,68824,423,9726,308,0001,051,0002,449,408
10Dec 28, 2025 19:0612,79023,860,4275,299,0002,119,0002,326,528
11Dec 28, 2025 19:0613,04131,565,1416,484,0001,080,0002,412,544
12Dec 28, 2025 19:0613,18126,973,9187,645,00002,408,448
13Dec 28, 2025 19:0613,34328,077,6606,634,0001,105,0002,363,392