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 20:18Wookyung JeongWookyung JeongScore: 62,778Success
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 20:2158,33865,188,43627,865,0005,971,0002,338,816
2Sep 27, 2025 20:2158,41262,486,72125,908,0007,971,0002,498,560
3Sep 27, 2025 20:2158,51656,845,64428,948,0004,991,0002,445,312
4Sep 27, 2025 20:2158,96457,107,31430,176,0004,023,0002,478,080
5Sep 27, 2025 20:1758,97657,125,53228,170,0006,036,0002,326,528
6Sep 27, 2025 20:2159,87664,373,02928,600,0006,128,0002,342,912
7Sep 27, 2025 20:2159,94559,641,09030,678,0004,090,0002,334,720
8Sep 27, 2025 20:2060,10966,248,07829,883,0004,980,0002,502,656
9Sep 27, 2025 20:1960,14860,935,13631,896,0002,990,0002,355,200
10Sep 27, 2025 20:2160,54556,936,07130,100,0005,016,0002,408,448
11Sep 27, 2025 20:1760,77956,688,95332,231,0003,021,0002,392,064
12Sep 27, 2025 20:2460,85961,677,21129,247,0006,051,0002,318,336
13Sep 27, 2025 20:2160,92662,993,16728,466,0006,871,0002,506,752
14Sep 27, 2025 20:2160,94060,495,92131,306,0004,039,0002,478,080
15Sep 27, 2025 20:1961,02957,980,15526,295,0009,102,0002,277,376
16Sep 27, 2025 20:2161,04756,891,25730,349,0005,058,0002,404,352
17Sep 27, 2025 20:2461,22257,023,20330,437,0005,072,0002,265,088
18Sep 27, 2025 20:2161,29758,043,48127,426,0008,126,0002,334,720
19Sep 27, 2025 20:2161,90961,457,67430,920,0004,987,0002,383,872
20Sep 27, 2025 20:2162,45057,912,33531,191,0005,030,0002,297,856
21Sep 27, 2025 20:2162,47258,046,40733,215,0003,019,0002,351,104
22Sep 27, 2025 20:2162,57163,688,13730,243,0006,048,0002,367,488
23Sep 27, 2025 20:1962,62160,266,45332,285,0004,035,0002,416,640
24Sep 27, 2025 20:2162,69861,633,76231,315,0005,050,0002,273,280
25Sep 27, 2025 20:1962,77861,290,37830,343,0006,068,0002,289,664
26Sep 27, 2025 20:2162,87159,956,14930,388,0006,077,0002,310,144
27Sep 27, 2025 20:2162,94858,730,50833,468,0003,042,0002,506,752
28Sep 27, 2025 20:2163,18358,738,83030,539,0006,107,0002,310,144
29Sep 27, 2025 20:1963,26963,096,44230,580,0006,116,0002,347,008
30Sep 27, 2025 20:2163,30766,675,69431,619,0005,099,0002,326,528
31Sep 27, 2025 20:2163,31463,024,67832,642,0004,080,0002,322,432
32Sep 27, 2025 20:2163,48664,833,69729,856,0006,966,0002,359,296
33Sep 27, 2025 20:2163,76463,250,57128,987,0007,996,0002,510,848
34Sep 27, 2025 20:1963,77263,956,50332,990,0003,998,0002,404,352
35Sep 27, 2025 20:1863,77659,895,65129,992,0006,998,0002,461,696
36Sep 27, 2025 20:1763,78363,337,77832,995,0003,999,0002,396,160
37Sep 27, 2025 20:2063,87264,305,11231,039,0006,007,0002,265,088
38Sep 27, 2025 20:1864,15060,826,49931,174,0006,033,0002,306,048
39Sep 27, 2025 20:2464,39759,413,12830,284,0007,066,0002,379,776
40Sep 27, 2025 20:2164,76063,485,33532,486,0005,075,0002,293,760
41Sep 27, 2025 20:2164,86464,954,14127,453,00010,168,0002,490,368
42Sep 27, 2025 20:1965,77966,159,16732,128,0006,024,0002,461,696
43Sep 27, 2025 20:1965,79862,383,34131,133,0007,030,0002,392,064
44Sep 27, 2025 20:2166,02264,015,82632,247,0006,046,0002,465,792
45Sep 27, 2025 20:1966,64364,718,68432,550,0006,103,0002,437,120
46Sep 27, 2025 20:1866,98165,832,08333,869,0004,980,0002,441,216
47Sep 27, 2025 20:2067,35767,873,95930,052,0009,015,0002,433,024
48Sep 27, 2025 20:2168,54063,227,93031,599,0008,154,0002,457,600