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 13, 2025 19:20Alexey IlyukhovAlexey IlyukhovScore: 82,129Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 13, 2025 19:2077,77471,264,64643,105,0002,004,0002,277,376
2Jul 13, 2025 19:2078,24774,638,27042,358,0003,025,0002,428,928
3Jul 13, 2025 19:2078,59570,477,40644,572,0001,013,0002,277,376
4Jul 13, 2025 19:2079,46268,311,34341,079,0005,009,0002,510,848
5Jul 13, 2025 19:2079,56269,118,69044,140,0002,006,0002,277,376
6Jul 13, 2025 19:2079,58867,139,09042,147,0004,014,0002,269,184
7Jul 13, 2025 19:2079,69166,652,51640,193,0006,028,0002,523,136
8Jul 13, 2025 19:2080,44172,787,93844,628,0002,028,0002,404,352
9Jul 13, 2025 19:2080,84070,235,05244,892,0001,995,0002,408,448
10Jul 13, 2025 19:2081,36269,824,54843,174,0004,016,0002,510,848
11Jul 13, 2025 19:2082,12969,945,72944,595,0003,040,0002,412,544
12Jul 13, 2025 19:2082,55277,850,26845,885,0001,995,0002,383,872
13Jul 13, 2025 19:2082,76769,692,05745,005,0003,000,0002,351,104
14Jul 13, 2025 19:2082,76770,469,04745,005,0003,000,0002,396,160
15Jul 13, 2025 19:2083,38369,646,43846,347,0002,015,0002,510,848
16Jul 13, 2025 19:2083,92275,221,78946,647,0002,028,0002,392,064
17Jul 13, 2025 19:2084,05270,163,45443,776,0004,974,0002,490,368
18Jul 13, 2025 19:2084,29178,583,25345,896,0002,993,0002,383,872
19Jul 13, 2025 19:2084,44570,918,31944,980,0003,998,0002,494,464
20Jul 13, 2025 19:2085,61073,211,00446,614,0003,040,0002,355,200
21Jul 13, 2025 19:2085,98885,934,81045,884,0003,989,0002,502,656