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 29, 2025 20:29Wookyung JeongWookyung JeongScore: 55,297Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 29, 2025 20:2851,71251,625,98227,994,0001,999,0002,297,856
2Sep 29, 2025 20:2951,99057,262,68427,139,0003,015,0002,301,952
3Sep 29, 2025 20:2852,09151,556,99027,192,0003,021,0002,473,984
4Sep 29, 2025 20:2952,33661,543,11327,320,0003,035,0002,281,472
5Sep 29, 2025 20:3052,54856,945,33227,431,0003,047,0002,404,352
6Sep 29, 2025 20:3052,55254,719,09729,464,0001,016,0002,428,928
7Sep 29, 2025 20:3053,22861,329,04626,889,0003,983,0002,306,048
8Sep 29, 2025 20:3053,24563,219,90027,894,0002,988,0002,482,176
9Sep 29, 2025 20:2953,26957,623,07029,900,000996,0002,445,312
10Sep 29, 2025 20:2653,36254,025,46828,954,0001,996,0002,433,024
11Sep 29, 2025 20:2953,45057,363,29630,001,0001,000,0002,469,888
12Sep 29, 2025 20:2653,53857,367,81829,049,0002,003,0002,498,560
13Sep 29, 2025 20:2853,56952,274,58529,066,0002,004,0002,465,792
14Sep 29, 2025 20:3253,79060,572,72627,173,0004,025,0002,486,272
15Sep 29, 2025 20:2853,82254,713,93328,196,0003,021,0002,314,240
16Sep 29, 2025 20:2253,96754,752,76028,272,0003,029,0002,400,256
17Sep 29, 2025 20:2554,07860,187,04430,354,0001,011,0002,314,240
18Sep 29, 2025 20:2854,12656,336,67729,368,0002,025,0002,301,952
19Sep 29, 2025 20:2554,13659,763,59627,348,0004,051,0002,334,720
20Sep 29, 2025 20:2654,18454,264,09129,400,0002,027,0002,314,240
21Sep 29, 2025 20:2254,51755,244,25129,580,0002,040,0002,375,680
22Sep 29, 2025 20:2954,55956,789,86128,582,0003,062,0002,342,912
23Sep 29, 2025 20:3254,62463,081,88829,638,0002,044,0002,265,088
24Sep 29, 2025 20:2754,65057,707,86030,675,0001,022,0002,383,872
25Sep 29, 2025 20:2654,98360,192,00726,747,0005,143,0002,490,368
26Sep 29, 2025 20:2255,05555,195,83027,941,0003,991,0002,482,176
27Sep 29, 2025 20:2955,10954,324,47928,967,0002,996,0002,502,656
28Sep 29, 2025 20:3055,29756,403,42329,066,0003,006,0002,314,240
29Sep 29, 2025 20:2755,43359,508,98629,137,0003,014,0002,506,752
30Sep 29, 2025 20:2955,50558,041,01631,187,0001,006,0002,400,256
31Sep 29, 2025 20:2855,52656,378,34530,193,0002,012,0002,306,048
32Sep 29, 2025 20:2555,59554,637,83131,238,0001,007,0002,478,080
33Sep 29, 2025 20:2555,60057,497,91930,233,0002,015,0002,453,504
34Sep 29, 2025 20:2855,62457,033,74930,246,0002,016,0002,289,664
35Sep 29, 2025 20:2955,74356,642,11128,290,0004,041,0002,277,376
36Sep 29, 2025 20:2956,03455,821,24628,438,0004,062,0002,428,928
37Sep 29, 2025 20:3056,12456,995,83129,501,0003,051,0002,469,888
38Sep 29, 2025 20:2556,44557,907,82531,715,0001,023,0002,301,952
39Sep 29, 2025 20:2756,69859,619,19630,892,0001,993,0002,338,816
40Sep 29, 2025 20:2956,70761,794,49231,894,000996,0002,424,832
41Sep 29, 2025 20:2956,73456,068,52428,918,0003,988,0002,506,752
42Sep 29, 2025 20:2956,79755,508,68427,951,0004,991,0002,510,848
43Sep 29, 2025 20:2957,12655,896,45232,129,0001,004,0002,490,368
44Sep 29, 2025 20:2857,19557,780,99131,163,0002,010,0002,355,200
45Sep 29, 2025 20:2957,25255,680,77730,188,0003,018,0002,326,528
46Sep 29, 2025 20:2557,34357,013,61430,236,0003,023,0002,379,776
47Sep 29, 2025 20:2957,46757,439,46231,311,0002,020,0002,281,472
48Sep 29, 2025 20:3257,51657,232,48631,338,0002,021,0002,371,584
49Sep 29, 2025 20:2958,16462,080,22431,751,0001,984,0002,338,816
50Sep 29, 2025 20:2658,40257,568,56329,888,0003,985,0002,338,816
51Sep 29, 2025 20:2859,07660,295,38232,249,0002,015,0002,502,656
52Sep 29, 2025 20:2959,26258,870,25631,340,0003,032,0002,367,488
53Sep 29, 2025 20:2959,60359,631,71332,537,0002,033,0002,260,992
54Sep 29, 2025 20:2659,65259,268,31833,581,0001,017,0002,306,048