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 listAug 18, 2025 21:19CadovvlCadovvlScore: 70,510Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 18, 2025 21:1969,61764,390,69939,369,0001,009,0002,260,992
2Aug 18, 2025 21:1969,82963,054,50939,489,0001,012,0002,506,752
3Aug 18, 2025 21:1570,06764,354,71140,639,00002,502,656
4Aug 18, 2025 21:1970,21468,166,29439,706,0001,018,0002,519,040
5Aug 18, 2025 21:1570,25267,307,37339,728,0001,018,0002,383,872
6Aug 18, 2025 21:1570,50568,020,19039,896,000997,0002,260,992
7Aug 18, 2025 21:1970,51066,894,80439,899,000997,0002,506,752
8Aug 18, 2025 21:1970,61465,282,39038,959,0001,997,0002,392,064
9Aug 18, 2025 21:1970,65072,917,37338,979,0001,998,0002,260,992
10Aug 18, 2025 21:1970,79767,061,80140,061,0001,001,0002,379,776
11Aug 18, 2025 21:1970,86267,134,52540,098,0001,002,0002,260,992
12Aug 18, 2025 21:1970,91467,337,63040,127,0001,003,0002,260,992