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 listDec 20, 2025 21:08idatsyidatsyScore: 64,012Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 21:1063,09161,628,46136,593,00002,318,336
2Dec 20, 2025 21:0563,17259,373,24135,623,0001,017,0002,318,336
3Dec 20, 2025 21:0563,26667,800,99836,694,00002,285,568
4Dec 20, 2025 21:0563,27262,555,85136,698,00002,326,528
5Dec 20, 2025 21:1063,58462,620,32934,886,0001,993,0002,314,240
6Dec 20, 2025 21:1063,87465,126,87535,045,0002,002,0002,494,464
7Dec 20, 2025 21:0863,91060,236,78535,065,0002,003,0002,277,376
8Dec 20, 2025 21:0564,01263,868,67535,121,0002,006,0002,314,240
9Dec 20, 2025 21:0564,04065,011,55136,140,0001,003,0002,330,624
10Dec 20, 2025 21:0564,12460,252,39835,182,0002,010,0002,322,432
11Dec 20, 2025 21:0564,47859,831,80336,387,0001,010,0002,490,368
12Dec 20, 2025 21:0565,63475,153,07136,116,0001,952,0002,494,464
13Dec 20, 2025 21:0865,72463,371,56337,117,0001,003,0002,273,280
14Dec 20, 2025 21:0566,28658,335,07537,435,0001,011,0002,379,776
15Dec 20, 2025 21:0866,81069,225,48838,750,00002,523,136