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 01:51Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 21:25Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 17, 2026 17:34011,626,264954,000028,672No answer to stdout. Died or killed?
2Dec 22, 2025 01:5036,13148,020,08618,961,0001,995,0002,510,848
3Dec 22, 2025 01:5036,14750,808,42819,967,000998,0002,514,944
4Dec 22, 2025 01:5036,30550,620,06719,052,0002,005,0002,457,600
5Dec 22, 2025 01:5137,21239,228,49720,556,0001,027,0002,400,256
6Dec 22, 2025 01:5137,42851,348,43220,675,0001,033,0002,412,544
7Dec 22, 2025 01:5037,76652,138,82919,913,0001,991,0002,510,848
8Dec 22, 2025 01:5137,83846,177,71520,949,000997,0002,506,752
9Dec 22, 2025 01:5137,91252,583,46720,990,000999,0002,342,912
10Dec 22, 2025 01:5037,92648,269,61020,998,000999,0002,490,368
11Dec 22, 2025 01:5138,10545,623,47620,092,0002,009,0002,359,296
12Dec 22, 2025 01:5138,11646,471,85621,103,0001,004,0002,355,200
13Dec 22, 2025 01:5038,67147,888,84921,410,0001,019,0002,502,656