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 14, 2025 18:00HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:13Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:18011,801,42001,658,00020,480No answer to stdout. Died or killed?
2Dec 14, 2025 18:00160,762117,558,69589,232,0004,010,0002,453,504
3Dec 14, 2025 18:00162,990119,352,08994,534,00002,433,024
4Dec 14, 2025 18:00163,026123,107,95292,544,0002,011,0002,428,928
5Dec 14, 2025 18:00163,803116,639,02294,006,0001,000,0002,404,352
6Dec 14, 2025 18:00164,717118,885,24094,531,0001,005,0002,478,080
7Dec 14, 2025 18:00167,259118,917,93892,010,0005,000,0002,428,928
8Dec 14, 2025 18:00170,983124,143,95797,167,0002,003,0002,400,256
9Dec 14, 2025 18:00176,848124,792,295100,561,0002,011,0002,371,584
10Dec 14, 2025 18:00181,152134,221,200102,067,0003,001,0002,433,024