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 listMar 20, 2026 14:44Dominique GarmierDominique GarmierScore: 19,188Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 13:3218,81431,200,4048,928,0001,984,0002,281,472
2Mar 20, 2026 14:0618,82428,650,2398,933,0001,985,0002,260,992
3Mar 20, 2026 12:4318,85230,151,5868,946,0001,988,0002,281,472
4Mar 20, 2026 13:3218,91429,230,5968,976,0001,994,0002,285,568
5Mar 20, 2026 13:0218,96627,850,35310,000,0001,000,0002,289,664
6Mar 20, 2026 13:0218,96725,691,5039,001,0002,000,0002,289,664
7Mar 20, 2026 13:4018,99128,056,9549,013,0002,002,0002,289,664
8Mar 20, 2026 13:4019,00331,773,6329,018,0002,004,0002,285,568
9Mar 20, 2026 13:0219,03329,179,19310,036,0001,003,0002,289,664
10Mar 20, 2026 12:5819,07227,912,6809,051,0002,011,0002,269,184
11Mar 20, 2026 12:1519,12426,674,8189,076,0002,016,0002,269,184
12Mar 20, 2026 12:5819,13130,826,9199,079,0002,017,0002,289,664
13Mar 20, 2026 12:0519,14532,641,58210,095,0001,009,0002,269,184
14Mar 20, 2026 14:0619,16628,648,46010,106,0001,010,0002,260,992
15Mar 20, 2026 12:0519,16931,294,5009,097,0002,021,0002,265,088
16Mar 20, 2026 14:0619,18830,135,9349,106,0002,023,0002,289,664
17Mar 20, 2026 14:4419,19827,868,3509,111,0002,024,0002,281,472
18Mar 20, 2026 12:4319,20327,387,81510,126,0001,012,0002,359,296
19Mar 20, 2026 12:2719,26928,800,6019,144,0002,032,0002,527,232
20Mar 20, 2026 13:3219,27928,786,9809,149,0002,033,0002,289,664
21Mar 20, 2026 12:1519,29127,003,0879,155,0002,034,0002,289,664
22Mar 20, 2026 12:1519,31225,953,6469,165,0002,036,0002,285,568
23Mar 20, 2026 12:2719,32827,230,4159,172,0002,038,0002,285,568
24Mar 20, 2026 13:4019,34534,067,4619,180,0002,040,0002,273,280
25Mar 20, 2026 12:0519,48429,753,7889,247,0002,054,0002,269,184
26Mar 20, 2026 12:5819,52830,353,67110,297,0001,029,0002,514,944
27Mar 20, 2026 12:4319,83135,858,2979,411,0002,091,0002,269,184
28Mar 20, 2026 14:4419,84728,379,32010,465,0001,046,0002,289,664
29Mar 20, 2026 14:4419,91431,880,39810,500,0001,050,0002,523,136
30Mar 20, 2026 12:2719,97128,414,75810,530,0001,053,0002,277,376