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 17:08E SequeiraE SequeiraScore: 71,847Success
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 17:0270,53462,571,72638,915,0001,995,0002,277,376
2May 25, 2025 17:0370,70766,777,38139,010,0002,000,0002,277,376
3May 25, 2025 17:0871,07963,693,37039,215,0002,011,0002,510,848
4May 25, 2025 17:0471,18867,284,44239,275,0002,014,0002,277,376
5May 25, 2025 17:0871,49365,881,77640,455,0001,011,0002,277,376
6May 25, 2025 17:0271,56762,314,22640,497,0001,012,0002,273,280
7May 25, 2025 17:0871,57164,180,86240,499,0001,012,0002,285,568
8May 25, 2025 17:0871,62868,083,43541,544,00002,277,376
9May 25, 2025 17:0271,63864,451,86440,537,0001,013,0002,285,568
10May 25, 2025 17:0271,71770,114,77040,582,0001,014,0002,506,752
11May 25, 2025 17:0271,74064,165,46540,595,0001,014,0002,277,376
12May 25, 2025 17:0271,74366,151,39641,611,00002,277,376
13May 25, 2025 17:0471,84770,358,07840,655,0001,016,0002,363,392
14May 25, 2025 17:0471,91970,174,51040,696,0001,017,0002,506,752
15May 25, 2025 17:0371,94566,804,50240,711,0001,017,0002,281,472
16May 25, 2025 17:0272,14571,821,92739,852,0001,992,0002,269,184
17May 25, 2025 17:0872,18466,430,86839,874,0001,993,0002,273,280
18May 25, 2025 17:0872,19070,251,51439,877,0001,993,0002,506,752
19May 25, 2025 17:0272,20267,992,98340,880,000997,0002,506,752
20May 25, 2025 17:0872,20270,703,02840,880,000997,0002,510,848
21May 25, 2025 17:0872,30567,167,50640,939,000998,0002,510,848
22May 25, 2025 17:0872,45374,339,05340,022,0002,001,0002,506,752
23May 25, 2025 17:0372,47264,415,07740,033,0002,001,0002,506,752
24May 25, 2025 17:0272,47867,216,35541,037,0001,000,0002,277,376