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 listMar 31, 2025 19:36Yuriy LyfenkoYuriy LyfenkoScore: 64,048Success
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 19:3555,73462,137,49831,316,0001,010,0002,510,848
2Mar 31, 2025 19:3557,34156,107,31431,243,0002,015,0002,453,504
3Mar 31, 2025 19:3557,53455,286,21532,359,0001,011,0002,289,664
4Mar 31, 2025 19:4757,55953,547,96433,384,00002,428,928
5Mar 31, 2025 19:3557,62861,384,47532,412,0001,012,0002,510,848
6Mar 31, 2025 19:4957,70357,296,62632,454,0001,014,0002,265,088
7Mar 31, 2025 19:3558,44865,994,02132,903,000997,0002,510,848
8Mar 31, 2025 19:4958,51456,240,40031,942,0001,996,0002,441,216
9Mar 31, 2025 19:3758,57956,703,63631,978,0001,998,0002,433,024
10Mar 31, 2025 19:3758,79759,861,99133,099,0001,003,0002,502,656
11Mar 31, 2025 19:3658,82156,853,48932,110,0002,006,0002,498,560
12Mar 31, 2025 19:3559,15559,259,41831,283,0003,027,0002,506,752
13Mar 31, 2025 19:3759,19560,166,43932,314,0002,019,0002,416,640
14Mar 31, 2025 19:4959,42957,290,88733,456,0001,013,0002,428,928
15Mar 31, 2025 19:3562,25558,405,68734,102,0002,006,0002,490,368
16Mar 31, 2025 19:3763,09065,913,90235,576,0001,016,0002,392,064
17Mar 31, 2025 19:3663,23169,532,94235,656,0001,018,0002,498,560
18Mar 31, 2025 19:4763,73664,158,16336,967,00002,453,504
19Mar 31, 2025 19:3764,04857,535,01435,140,0002,008,0002,482,176
20Mar 31, 2025 19:4965,34366,610,55935,905,0001,994,0002,420,736
21Mar 31, 2025 19:4765,34763,062,89636,904,000997,0002,396,160
22Mar 31, 2025 19:4765,91260,124,19336,217,0002,012,0002,486,272
23Mar 31, 2025 19:4966,33465,863,12537,462,0001,012,0002,478,080
24Mar 31, 2025 19:3767,00366,532,83836,870,0001,992,0002,502,656
25Mar 31, 2025 19:3567,06661,622,49936,904,0001,994,0002,469,888
26Mar 31, 2025 19:4767,84366,739,00537,332,0002,017,0002,383,872
27Mar 31, 2025 19:3568,03663,943,76438,450,0001,011,0002,449,408
28Mar 31, 2025 19:4768,77965,841,45238,895,000997,0002,465,792
29Mar 31, 2025 19:4968,97463,429,03137,005,0003,000,0002,437,120
30Mar 31, 2025 19:3669,37469,497,32838,226,0002,011,0002,400,256
31Mar 31, 2025 19:48103,21990,122,62258,870,000997,0002,428,928
32Mar 31, 2025 19:36104,58685,926,91760,660,00002,441,216
33Mar 31, 2025 19:48105,35583,249,86559,103,0002,003,0002,514,944
34Mar 31, 2025 19:36107,13484,983,88961,136,0001,002,0002,482,176
35Mar 31, 2025 19:36110,94187,601,54363,341,0001,005,0002,375,680
36Mar 31, 2025 19:48114,72989,801,60365,535,0001,008,0002,478,080