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 listJul 16, 2025 05:36CadovvlCadovvlScore: 73,947Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 15, 2025 12:5773,00069,040,61040,324,0002,016,0002,285,568
2Jul 15, 2025 12:5773,38866,469,60541,552,0001,013,0002,285,568
3Jul 15, 2025 12:5773,61466,548,36341,680,0001,016,0002,519,040
4Jul 20, 2025 17:4273,68476,421,90742,737,00002,260,992
5Jul 16, 2025 05:3673,69168,910,20341,724,0001,017,0002,277,376
6Jul 15, 2025 12:5773,71470,663,43742,754,00002,285,568
7Jul 15, 2025 12:5773,72467,324,25941,742,0001,018,0002,519,040
8Jul 20, 2025 17:4273,94767,259,02640,895,0001,994,0002,277,376
9Jul 16, 2025 05:3673,97174,971,51340,908,0001,995,0002,285,568
10Jul 15, 2025 12:5774,02969,986,39340,940,0001,997,0002,519,040
11Jul 15, 2025 12:5774,10773,227,45740,983,0001,999,0002,379,776
12Jul 15, 2025 12:5774,15368,487,31542,009,0001,000,0002,285,568
13Jul 15, 2025 12:5774,23167,690,46342,053,0001,001,0002,277,376
14Jul 16, 2025 05:3674,24579,913,12641,060,0002,002,0002,277,376
15Jul 20, 2025 17:4274,29868,725,88041,089,0002,004,0002,351,104