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:30CadovvlCadovvlScore: 70,512Success
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:3069,95562,951,95639,560,0001,014,0002,256,896
2Aug 18, 2025 21:3069,95964,274,48340,576,00002,519,040
3Aug 18, 2025 21:3170,21768,751,95340,726,00002,387,968
4Aug 18, 2025 21:3170,48869,452,28738,889,0001,994,0002,260,992
5Aug 18, 2025 21:3070,49168,941,12040,885,00002,519,040
6Aug 18, 2025 21:3070,49873,734,53539,892,000997,0002,260,992
7Aug 18, 2025 21:3170,50566,334,82138,899,0001,994,0002,387,968
8Aug 18, 2025 21:3070,51265,792,42938,902,0001,995,0002,260,992
9Aug 18, 2025 21:3170,51273,064,46439,900,000997,0002,383,872
10Aug 18, 2025 21:3070,61668,185,29138,960,0001,997,0002,260,992
11Aug 18, 2025 21:3070,62470,499,75639,963,000999,0002,387,968
12Aug 18, 2025 21:3070,78669,763,61739,054,0002,002,0002,260,992
13Aug 18, 2025 21:3170,78667,768,09840,055,0001,001,0002,519,040
14Aug 18, 2025 21:3170,81971,181,68539,072,0002,003,0002,260,992
15Aug 18, 2025 21:3070,82269,168,04140,076,0001,001,0002,260,992