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 25, 2025 01:22Josu San MartinJosu San MartinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 15, 2026 12:21Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 15, 2026 12:24010,630,977700,000020,480No answer to stdout. Died or killed?
2Dec 25, 2025 01:2733,40342,038,04418,355,0001,019,0002,502,656
3Dec 25, 2025 01:2633,53144,781,72118,425,0001,023,0002,441,216
4Dec 25, 2025 01:2633,79143,599,66518,568,0001,031,0002,322,432
5Dec 25, 2025 01:2634,46036,234,83818,988,000999,0002,478,080
6Dec 25, 2025 01:2335,31242,956,32419,457,0001,024,0002,367,488
7Dec 25, 2025 01:2635,41037,564,72119,512,0001,026,0002,392,064
8Dec 25, 2025 01:2635,56444,913,42219,596,0001,031,0002,301,952
9Dec 25, 2025 01:2635,66251,290,19319,650,0001,034,0002,281,472
10Dec 25, 2025 01:2335,66450,972,94819,651,0001,034,0002,293,760
11Dec 25, 2025 01:2636,03652,284,32118,911,0001,990,0002,433,024
12Dec 25, 2025 01:2636,22147,351,63918,007,0003,001,0002,367,488
13Dec 25, 2025 01:2636,32444,472,59219,062,0002,006,0002,416,640
14Dec 25, 2025 01:2336,32844,127,41520,067,0001,003,0002,338,816
15Dec 25, 2025 01:2236,36447,084,53920,087,0001,004,0002,494,464
16Dec 25, 2025 01:2736,48644,665,61820,155,0001,007,0002,396,160
17Dec 25, 2025 01:2236,51737,083,64420,172,0001,008,0002,363,392
18Dec 25, 2025 01:2236,53348,357,12520,180,0001,009,0002,265,088
19Dec 25, 2025 01:2737,00947,729,84020,443,0001,022,0002,412,544