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 listMay 25, 2025 14:16E SequeiraE SequeiraScore: 73,059Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1May 25, 2025 16:2772,32464,621,82539,951,0001,997,0002,510,848
2May 25, 2025 14:1272,60564,862,26540,106,0002,005,0002,277,376
3May 25, 2025 14:1672,61666,291,12541,115,0001,002,0002,510,848
4May 25, 2025 14:1272,75064,978,27240,186,0002,009,0002,273,280
5May 25, 2025 14:1672,78471,120,67741,210,0001,005,0002,273,280
6May 25, 2025 14:1672,80774,312,07040,218,0002,010,0002,277,376
7May 25, 2025 16:2772,87268,424,06440,254,0002,012,0002,359,296
8May 25, 2025 16:3772,89870,598,51040,268,0002,013,0002,285,568
9May 25, 2025 16:3772,94069,585,70540,291,0002,014,0002,289,664
10May 25, 2025 14:1673,05965,347,19941,366,0001,008,0002,519,040
11May 25, 2025 14:1673,06664,554,70641,369,0001,009,0002,351,104
12May 25, 2025 14:1673,12869,293,98041,405,0001,009,0002,273,280
13May 25, 2025 14:1673,16466,855,33341,425,0001,010,0002,510,848
14May 25, 2025 14:1673,23470,224,42941,465,0001,011,0002,510,848
15May 25, 2025 14:1673,34770,247,93441,529,0001,012,0002,506,752
16May 25, 2025 16:2773,38671,596,28541,551,0001,013,0002,269,184
17May 25, 2025 14:1273,42465,869,18141,573,0001,013,0002,273,280
18May 25, 2025 16:3773,45069,822,77741,587,0001,014,0002,506,752