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 listSep 4, 2025 16:21Eli RiggsEli RiggsScore: 1,292,712Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 4, 2025 17:271,260,476757,381,369730,076,0001,000,0002,289,664
2Sep 4, 2025 16:211,263,022756,129,609730,555,0001,998,0002,342,912
3Sep 4, 2025 17:271,267,003761,971,899732,863,0001,999,0002,273,280
4Sep 4, 2025 17:271,274,438765,929,615736,174,0003,000,0002,330,624
5Sep 4, 2025 16:211,276,479772,054,872735,363,0004,995,0002,273,280
6Sep 4, 2025 17:271,279,212767,818,613740,944,000999,0002,277,376
7Sep 4, 2025 17:271,284,490772,107,093743,004,0002,000,0002,310,144
8Sep 4, 2025 17:271,286,916774,701,928744,413,0001,998,0002,273,280
9Sep 4, 2025 17:271,287,052769,779,840743,493,0002,997,0002,265,088
10Sep 4, 2025 16:211,292,712776,689,734745,775,0003,998,0002,482,176
11Sep 4, 2025 17:271,296,829775,409,696751,161,0001,000,0002,277,376
12Sep 4, 2025 16:211,297,126780,767,074750,335,0001,998,0002,494,464
13Sep 4, 2025 16:211,302,569784,125,265752,493,0002,997,0002,490,368
14Sep 4, 2025 16:211,310,278786,857,209755,962,0003,999,0002,490,368
15Sep 4, 2025 16:211,311,174784,874,988757,484,0002,997,0002,273,280
16Sep 4, 2025 16:211,312,310791,376,761759,140,0002,000,0002,482,176
17Sep 4, 2025 17:271,312,529789,053,886759,267,0002,000,0002,277,376
18Sep 4, 2025 16:211,325,560792,326,751765,826,0002,999,0002,273,280