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 listSep 27, 2025 14:27Wookyung JeongWookyung JeongScore: 84,262Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2025 14:2775,60368,456,42937,871,0005,979,0002,379,776
2Sep 27, 2025 14:2776,00067,195,41139,071,0005,009,0002,502,656
3Sep 27, 2025 14:2777,77167,874,96437,088,0008,019,0002,424,832
4Sep 27, 2025 14:2778,17270,300,55240,303,0005,037,0002,322,432
5Sep 27, 2025 14:2778,62167,466,30740,534,0005,066,0002,465,792
6Sep 27, 2025 14:2779,29869,218,24240,994,0004,999,0002,326,528
7Sep 27, 2025 14:2780,26267,504,44537,444,0009,108,0002,318,336
8Sep 27, 2025 14:2780,37868,837,82541,552,0005,067,0002,510,848
9Sep 27, 2025 14:2780,99370,886,27840,979,0005,997,0002,523,136
10Sep 27, 2025 14:2781,15370,347,67040,059,0007,010,0002,351,104
11Sep 27, 2025 14:2781,55269,049,67441,262,0006,038,0002,498,560
12Sep 27, 2025 14:2782,65268,689,19242,945,0004,993,0002,420,736
13Sep 27, 2025 14:2782,86672,186,30041,053,0007,009,0002,519,040
14Sep 27, 2025 14:2782,87171,901,53539,053,0009,012,0002,498,560
15Sep 27, 2025 14:2783,49380,125,18142,373,0006,053,0002,400,256
16Sep 27, 2025 14:2784,25075,730,04939,890,0008,975,0002,273,280
17Sep 27, 2025 14:2784,26270,543,85741,891,0006,981,0002,310,144
18Sep 27, 2025 14:2784,36081,686,87943,937,0004,992,0002,338,816
19Sep 27, 2025 14:2785,18374,321,76043,357,0006,049,0002,265,088
20Sep 27, 2025 14:2786,01071,191,87840,907,0008,979,0002,355,200
21Sep 27, 2025 14:2786,07674,436,66142,935,0006,989,0002,392,064
22Sep 27, 2025 14:2786,97669,976,83242,375,0008,071,0002,310,144
23Sep 27, 2025 14:2786,99076,670,42942,382,0008,072,0002,461,696
24Sep 27, 2025 14:2787,72974,783,44646,893,0003,990,0002,301,952
25Sep 27, 2025 14:2787,74172,464,69841,910,0008,980,0002,486,272
26Sep 27, 2025 14:2788,11774,532,44940,085,00011,023,0002,412,544
27Sep 27, 2025 14:2788,42274,859,77448,269,0003,016,0002,326,528
28Sep 27, 2025 14:2789,45276,844,75444,898,0006,984,0002,428,928
29Sep 27, 2025 14:2789,55373,368,27346,947,0004,994,0002,478,080
30Sep 27, 2025 14:2790,18375,095,53345,265,0007,041,0002,383,872
31Sep 27, 2025 14:2790,48174,639,18544,406,0008,073,0002,314,240
32Sep 27, 2025 14:2792,08379,081,91042,324,00011,084,0002,469,888
33Sep 27, 2025 14:2793,89077,306,49747,397,0007,059,0002,449,408