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 listSep 11, 2025 16:30caandocaandoError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:01Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:09084,502,30068,535,0004,624,0002,113,536expected "862851750 ", got "7000 "
2Oct 5, 2026 19:09083,592,24268,524,0004,543,0002,113,536expected "871792527 ", got "1024000 "
3Oct 5, 2026 19:09084,526,44669,368,0004,697,0002,113,536expected "863816838 ", got "8000 "
4Sep 11, 2025 16:30116,34389,770,98563,451,0004,028,0002,424,832
5Sep 11, 2025 16:30116,54193,153,29764,568,0003,026,0002,510,848
6Sep 11, 2025 16:30117,32993,151,83365,049,0003,002,0002,330,624
7Sep 11, 2025 16:30117,34590,765,04765,058,0003,002,0002,322,432
8Sep 11, 2025 16:30118,44097,464,70864,655,0004,040,0002,375,680
9Sep 11, 2025 16:30119,34795,694,43365,209,0004,012,0002,379,776
10Sep 11, 2025 16:30119,76098,998,14066,441,0003,020,0002,412,544
11Sep 11, 2025 16:30121,30394,461,35966,336,0004,020,0002,408,448
12Sep 11, 2025 16:30122,23493,355,34865,904,0004,992,0002,330,624