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 listApr 1, 2025 13:28Yuriy LyfenkoYuriy LyfenkoScore: 58,093Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 31, 2025 23:0451,75557,533,37629,018,0001,000,0002,486,272
2Mar 31, 2025 23:0552,75952,982,28229,580,0001,020,0002,469,888
3Mar 31, 2025 23:0452,82163,810,87330,636,00002,523,136
4Mar 31, 2025 23:0453,28155,654,83429,907,000996,0002,334,720
5Mar 31, 2025 23:0453,55952,697,61930,062,0001,002,0002,498,560
6Mar 31, 2025 23:0554,99759,736,04930,902,000996,0002,310,144
7Mar 31, 2025 23:0555,07254,326,35429,946,0001,996,0002,326,528
8Mar 31, 2025 23:0555,80053,741,84132,364,00002,342,912
9Mar 31, 2025 23:0556,10260,120,40132,539,00002,478,080
10Mar 31, 2025 23:0556,70563,743,20430,896,0001,993,0002,523,136
11Mar 31, 2025 23:0457,10756,357,48432,119,0001,003,0002,478,080
12Mar 31, 2025 23:0457,77654,606,25733,510,00002,326,528
13Mar 31, 2025 23:0558,09362,399,22932,673,0001,021,0002,461,696
14Mar 31, 2025 23:0558,09359,818,52332,673,0001,021,0002,379,776
15Mar 31, 2025 23:0458,86457,620,41733,137,0001,004,0002,433,024
16Mar 31, 2025 23:0460,35757,190,81334,007,0001,000,0002,531,328
17Mar 31, 2025 23:0460,42858,128,55134,047,0001,001,0002,453,504
18Mar 31, 2025 23:0560,48660,499,42434,080,0001,002,0002,531,328
19Mar 31, 2025 23:0561,03162,822,90234,387,0001,011,0002,449,408
20Mar 31, 2025 23:0561,32857,559,22434,554,0001,016,0002,269,184
21Mar 31, 2025 23:0565,33364,354,05436,896,000997,0002,490,368
22Apr 1, 2025 13:2899,36991,260,27356,623,0001,011,0002,510,848
23Apr 1, 2025 13:28101,07284,445,08257,612,0001,010,0002,482,176
24Apr 1, 2025 13:28107,78684,818,58662,516,00002,297,856