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 7, 2025 13:22CadovvlCadovvlScore: 70,903Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 8, 2025 10:4569,81969,193,81539,483,0001,012,0002,387,968
2Aug 7, 2025 13:1670,10064,584,50639,642,0001,016,0002,383,872
3Aug 7, 2025 13:2270,54367,156,10439,918,000997,0002,383,872
4Aug 7, 2025 13:1670,62172,834,54738,962,0001,998,0002,387,968
5Aug 8, 2025 10:4570,72870,529,35939,021,0002,001,0002,256,896
6Aug 7, 2025 13:1670,74163,133,69839,029,0002,001,0002,379,776
7Aug 7, 2025 13:1670,77267,099,81939,046,0002,002,0002,510,848
8Aug 7, 2025 13:1670,77271,051,51240,047,0001,001,0002,379,776
9Aug 7, 2025 13:1670,89767,587,64340,118,0001,002,0002,510,848
10Aug 8, 2025 10:4570,89870,188,50439,116,0002,005,0002,256,896
11Aug 8, 2025 10:4570,90367,323,70739,118,0002,006,0002,510,848
12Aug 8, 2025 10:4570,91770,304,13839,126,0002,006,0002,379,776
13Aug 7, 2025 13:1670,94566,464,64339,141,0002,007,0002,400,256
14Aug 8, 2025 10:4571,02973,386,21439,188,0002,009,0002,510,848
15Aug 7, 2025 13:2271,15568,502,78440,264,0001,006,0002,392,064
16Aug 8, 2025 10:4571,16768,431,26440,271,0001,006,0002,519,040
17Aug 7, 2025 13:1671,19365,410,97540,285,0001,007,0002,387,968
18Aug 8, 2025 10:4571,22175,740,88241,308,00002,375,680
19Aug 7, 2025 13:2271,24568,907,41340,315,0001,007,0002,387,968
20Aug 7, 2025 13:1671,28465,951,14340,337,0001,008,0002,387,968
21Aug 8, 2025 10:4571,62662,526,19541,543,00002,387,968