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 30, 2025 16:21Wookyung JeongWookyung JeongScore: 55,412Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 30, 2025 16:2048,21650,269,88523,970,0003,995,0002,322,432
2Sep 30, 2025 16:2249,39154,826,27026,601,0002,046,0002,379,776
3Sep 30, 2025 16:2049,89554,514,03925,946,0002,993,0002,269,184
4Sep 30, 2025 16:2150,67952,109,34126,354,0003,040,0002,322,432
5Sep 30, 2025 16:2150,73455,225,55226,382,0003,044,0002,334,720
6Sep 30, 2025 16:2151,64154,323,37925,959,0003,993,0002,301,952
7Sep 30, 2025 16:2152,07952,719,15327,186,0003,020,0002,326,528
8Sep 30, 2025 16:2052,30050,901,66525,279,0005,055,0002,371,584
9Sep 30, 2025 16:2052,82158,079,75826,552,0004,084,0002,433,024
10Sep 30, 2025 16:2052,85562,041,94429,635,0001,021,0002,420,736
11Sep 30, 2025 16:2152,95757,032,36728,668,0002,047,0002,367,488
12Sep 30, 2025 16:2053,00757,141,00328,695,0002,049,0002,519,040
13Sep 30, 2025 16:2153,35952,785,84027,953,0002,995,0002,334,720
14Sep 30, 2025 16:2253,68856,382,75327,121,0004,018,0002,416,640
15Sep 30, 2025 16:2055,07154,847,34928,947,0002,994,0002,355,200
16Sep 30, 2025 16:2155,41259,538,75929,126,0003,013,0002,441,216
17Sep 30, 2025 16:2155,47961,291,10928,156,0004,022,0002,396,160
18Sep 30, 2025 16:2055,90954,732,25429,387,0003,040,0002,392,064
19Sep 30, 2025 16:2155,98458,098,61629,427,0003,044,0002,519,040
20Sep 30, 2025 16:2156,00959,185,12529,440,0003,045,0002,347,008
21Sep 30, 2025 16:2156,25761,587,06728,551,0004,078,0002,379,776
22Sep 30, 2025 16:2056,67659,987,36629,884,0002,988,0002,494,464
23Sep 30, 2025 16:2156,72856,691,43227,917,0004,985,0002,424,832
24Sep 30, 2025 16:2057,34754,336,85231,246,0002,015,0002,342,912
25Sep 30, 2025 16:2157,36254,046,28829,238,0004,032,0002,494,464
26Sep 30, 2025 16:2058,09567,815,84131,653,0002,042,0002,330,624
27Sep 30, 2025 16:2158,53660,607,88529,957,0003,994,0002,437,120
28Sep 30, 2025 16:2058,69361,275,02031,039,0003,003,0002,502,656
29Sep 30, 2025 16:2158,76259,830,56231,075,0003,007,0002,338,816
30Sep 30, 2025 16:2259,15059,029,66331,280,0003,027,0002,510,848