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 listMar 15, 2026 10:00Dominique GarmierDominique GarmierScore: 72,272Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 14, 2026 19:1771,08469,726,97739,218,0002,011,0002,506,752
2Mar 14, 2026 19:1771,11066,353,17939,233,0002,011,0002,498,560
3Mar 14, 2026 23:0971,65062,776,63740,544,0001,013,0002,506,752
4Mar 14, 2026 23:0971,67166,325,11040,556,0001,013,0002,396,160
5Mar 15, 2026 10:0871,81267,618,48640,636,0001,015,0002,392,064
6Mar 15, 2026 10:0871,81268,090,63441,651,00002,498,560
7Mar 14, 2026 19:2071,82165,981,72140,640,0001,016,0002,396,160
8Mar 15, 2026 10:0071,84071,001,04840,651,0001,016,0002,506,752
9Mar 14, 2026 23:0972,21066,665,81639,888,0001,994,0002,383,872
10Mar 14, 2026 19:2072,27266,875,27339,922,0001,996,0002,392,064
11Mar 14, 2026 19:2072,49768,506,53940,046,0002,002,0002,342,912
12Mar 15, 2026 10:0872,59569,652,69441,103,0001,002,0002,506,752
13Mar 15, 2026 09:4972,71268,012,21440,165,0002,008,0002,392,064
14Mar 15, 2026 09:4972,81069,260,34141,225,0001,005,0002,486,272
15Mar 14, 2026 19:1772,92467,624,97440,282,0002,014,0002,260,992
16Mar 15, 2026 10:0073,00070,384,36541,332,0001,008,0002,494,464
17Mar 15, 2026 10:0073,59370,264,18341,668,0001,016,0002,498,560
18Mar 15, 2026 09:4973,83473,151,60542,824,00002,375,680