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 listFeb 27, 2026 23:260xZohan0xZohanScore: 125,333Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 27, 2026 23:22123,67897,836,25068,702,0003,031,0002,293,760
2Feb 27, 2026 23:26123,87198,887,15168,852,0002,993,0002,486,272
3Feb 27, 2026 23:26124,007100,417,15667,929,0003,995,0002,433,024
4Feb 27, 2026 23:26124,340101,055,03568,111,0004,006,0002,502,656
5Feb 27, 2026 23:22124,497103,702,22167,194,0005,014,0002,355,200
6Feb 27, 2026 23:26124,574104,745,97667,236,0005,017,0002,322,432
7Feb 27, 2026 23:22124,70798,420,38268,312,0004,018,0002,330,624
8Feb 27, 2026 23:22125,00395,056,06768,475,0004,027,0002,293,760
9Feb 27, 2026 23:26125,07198,208,61268,511,0004,030,0002,277,376
10Feb 27, 2026 23:26125,20296,330,78868,583,0004,034,0002,351,104
11Feb 27, 2026 23:22125,333103,105,03267,645,0005,048,0002,383,872
12Feb 27, 2026 23:22125,44899,081,26365,783,0006,977,0002,363,392
13Feb 27, 2026 23:22125,466106,197,10066,789,0005,981,0002,510,848
14Feb 27, 2026 23:26125,529105,051,62365,826,0006,981,0002,441,216
15Feb 27, 2026 23:26125,593100,183,66967,855,0004,989,0002,400,256
16Feb 27, 2026 23:26125,617100,522,59567,868,0004,990,0002,379,776
17Feb 27, 2026 23:22125,652100,676,77369,883,0002,995,0002,367,488
18Feb 27, 2026 23:26126,043102,164,23469,100,0004,005,0002,359,296
19Feb 27, 2026 23:26126,117102,635,12068,138,0005,010,0002,334,720
20Feb 27, 2026 23:22126,15397,908,17567,156,0006,013,0002,338,816
21Feb 27, 2026 23:26126,52498,618,27667,353,0006,031,0002,449,408