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 11, 2025 08:11CadovvlCadovvlScore: 70,800Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 11, 2025 08:1169,81660,648,37240,493,00002,510,848
2Aug 11, 2025 07:3770,50969,666,43839,898,000997,0002,519,040
3Aug 11, 2025 07:3770,52464,353,04938,909,0001,995,0002,387,968
4Aug 11, 2025 07:3870,52468,902,04838,909,0001,995,0002,510,848
5Aug 11, 2025 07:4070,59172,359,81739,945,000998,0002,519,040
6Aug 11, 2025 07:4070,62669,120,36239,964,000999,0002,519,040
7Aug 11, 2025 08:1170,65069,600,49139,978,000999,0002,379,776
8Aug 11, 2025 07:3870,67670,757,67238,993,0001,999,0002,383,872
9Aug 11, 2025 07:3970,69370,400,75240,002,0001,000,0002,506,752
10Aug 11, 2025 07:3870,71668,065,67540,015,0001,000,0002,510,848
11Aug 11, 2025 07:3970,80070,697,32839,061,0002,003,0002,383,872
12Aug 11, 2025 07:3970,95565,950,40539,147,0002,007,0002,510,848
13Aug 11, 2025 07:4271,04768,483,62440,202,0001,005,0002,510,848
14Aug 11, 2025 07:4271,07264,650,98139,212,0002,010,0002,387,968
15Aug 11, 2025 07:4071,08867,751,84240,226,0001,005,0002,392,064
16Aug 11, 2025 08:1171,15962,240,72339,259,0002,013,0002,506,752
17Aug 11, 2025 07:3771,18672,332,69440,281,0001,007,0002,519,040
18Aug 11, 2025 08:1171,21971,276,80939,292,0002,015,0002,510,848
19Aug 11, 2025 08:1171,24862,030,30041,324,00002,498,560
20Aug 11, 2025 07:4271,28863,188,69140,339,0001,008,0002,510,848
21Aug 11, 2025 08:1171,43465,107,97440,422,0001,010,0002,371,584