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 10, 2026 13:35Code HintsCode HintsScore: 80,474Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 10, 2026 13:2979,88175,341,83945,324,0001,007,0002,371,584
2Feb 10, 2026 13:5880,00368,804,69546,402,00002,478,080
3Feb 10, 2026 14:0780,04768,085,70145,418,0001,009,0002,523,136
4Feb 10, 2026 13:2980,11668,903,61145,457,0001,010,0002,281,472
5Feb 10, 2026 14:1380,14869,195,27045,476,0001,010,0002,514,944
6Feb 10, 2026 13:4480,14872,476,33346,486,00002,314,240
7Feb 10, 2026 13:4480,18367,663,12645,495,0001,011,0002,281,472
8Feb 10, 2026 13:3680,20374,865,17345,507,0001,011,0002,269,184
9Feb 10, 2026 13:3680,25070,389,78245,534,0001,011,0002,494,464
10Feb 10, 2026 14:1380,26666,729,65845,542,0001,012,0002,301,952
11Feb 10, 2026 14:1380,30269,209,72546,575,00002,301,952
12Feb 10, 2026 13:3580,32168,033,42545,574,0001,012,0002,306,048
13Feb 10, 2026 13:2980,35372,449,01245,592,0001,013,0002,281,472
14Feb 10, 2026 13:5880,38679,266,09446,624,00002,306,048
15Feb 10, 2026 14:0780,41673,407,49445,628,0001,013,0002,510,848
16Feb 10, 2026 13:3680,41978,097,15446,643,00002,510,848
17Feb 10, 2026 13:5880,47472,073,63145,661,0001,014,0002,273,280
18Feb 10, 2026 14:0480,47871,242,80446,677,00002,297,856
19Feb 10, 2026 13:3680,50773,004,57545,679,0001,015,0002,306,048
20Feb 10, 2026 13:5880,84875,021,60444,897,0001,995,0002,478,080
21Feb 10, 2026 14:0480,85079,558,67945,896,000997,0002,510,848
22Feb 10, 2026 14:1080,85074,313,99844,898,0001,995,0002,281,472
23Feb 10, 2026 14:1080,85069,654,05444,898,0001,995,0002,490,368
24Feb 10, 2026 13:5880,86074,548,02646,899,00002,527,232
25Feb 10, 2026 13:3680,89371,378,09744,922,0001,996,0002,306,048
26Feb 10, 2026 13:3681,05371,433,05446,011,0001,000,0002,297,856
27Feb 10, 2026 14:0481,07275,380,85345,022,0002,000,0002,281,472
28Feb 10, 2026 13:5881,09076,671,58746,032,0001,000,0002,293,760
29Feb 10, 2026 13:3581,14872,954,03446,065,0001,001,0002,408,448
30Feb 10, 2026 14:0781,15075,529,26247,067,00002,310,144
31Feb 10, 2026 13:4481,19069,674,79046,089,0001,001,0002,310,144
32Feb 10, 2026 13:3581,23874,409,40746,116,0001,002,0002,310,144
33Feb 10, 2026 14:1081,32674,764,69645,162,0002,007,0002,301,952