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 05:41Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:27Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:37010,656,170717,000020,480No answer to stdout. Died or killed?
2Dec 23, 2025 05:4135,38842,929,00419,499,0001,026,0002,506,752
3Dec 23, 2025 05:4136,32847,317,18219,064,0002,006,0002,519,040
4Dec 23, 2025 05:4536,34053,892,03819,070,0002,007,0002,453,504
5Dec 23, 2025 05:4236,34148,962,88719,071,0002,007,0002,469,888
6Dec 23, 2025 05:4136,34344,333,59619,072,0002,007,0002,506,752
7Dec 23, 2025 05:4236,49743,090,42220,160,0001,008,0002,457,600
8Dec 23, 2025 05:4136,74339,890,60421,311,00002,506,752
9Dec 23, 2025 05:4536,77438,741,96620,314,0001,015,0002,441,216
10Dec 23, 2025 05:4536,89745,482,83821,400,00002,461,696
11Dec 23, 2025 05:4136,93343,281,43819,381,0002,040,0002,453,504
12Dec 23, 2025 05:4137,08144,456,04720,483,0001,024,0002,428,928
13Dec 23, 2025 05:4137,15346,346,87521,549,00002,293,760
14Dec 23, 2025 05:4137,32648,128,29820,619,0001,030,0002,367,488
15Dec 23, 2025 05:4237,33650,448,24221,655,00002,461,696
16Dec 23, 2025 05:4137,70553,056,94619,881,0001,988,0002,338,816