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 1, 2025 14:06Code HintsCode HintsScore: 93,040Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 1, 2025 20:5491,47881,318,30251,055,0002,002,0002,412,544
2Feb 1, 2025 15:2791,51281,823,71651,075,0002,002,0002,535,424
3Feb 1, 2025 20:5491,59073,744,17351,118,0002,004,0002,420,736
4Feb 1, 2025 14:0692,03385,672,50352,372,0001,007,0002,535,424
5Feb 1, 2025 14:2192,10777,195,41452,415,0001,007,0002,273,280
6Feb 1, 2025 15:3192,13378,817,15053,437,00002,273,280
7Feb 1, 2025 15:2792,19780,680,08952,466,0001,008,0002,535,424
8Feb 1, 2025 15:3192,21284,018,62051,465,0002,018,0002,535,424
9Feb 1, 2025 20:4992,24074,185,73153,499,00002,273,280
10Feb 1, 2025 20:5492,36294,954,28252,560,0001,010,0002,269,184
11Feb 1, 2025 20:5492,48174,842,59852,627,0001,012,0002,535,424
12Feb 1, 2025 15:2792,95582,214,34951,918,0001,996,0002,535,424
13Feb 1, 2025 14:2193,04080,034,92852,964,000999,0002,273,280
14Feb 1, 2025 15:3193,09585,783,85352,996,000999,0002,273,280
15Feb 1, 2025 20:5493,31077,576,23253,118,0001,002,0002,273,280
16Feb 1, 2025 14:0693,40577,700,90052,169,0002,006,0002,412,544
17Feb 1, 2025 20:5493,56977,391,71452,260,0002,010,0002,535,424
18Feb 1, 2025 20:5493,83176,598,61254,422,00002,412,544
19Feb 1, 2025 20:5494,09093,792,17353,562,0001,010,0002,265,088
20Feb 1, 2025 20:4994,24580,525,46753,650,0001,012,0002,535,424
21Feb 1, 2025 20:5494,61481,257,73753,879,000997,0002,408,448
22Feb 1, 2025 14:0694,63180,378,65052,891,0001,995,0002,273,280
23Feb 1, 2025 14:2194,63880,350,13054,890,00002,416,640
24Feb 1, 2025 20:4994,99383,439,42753,093,0002,003,0002,273,280