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 listDec 25, 2025 11:27HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:47Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:4802,767,564,9802,759,646,00002,117,632expected "999999000 ", got "100000 "
2Jul 5, 2026 20:4802,768,055,9382,758,736,0001,242,0002,117,632expected "999999000 ", got "100000 "
3Jul 5, 2026 20:4802,769,526,9062,759,842,00002,117,632expected "999999000 ", got "100000 "
4Dec 25, 2025 11:2428,72237,509,67816,659,00002,523,136
5Dec 25, 2025 09:5328,76442,981,18516,683,00002,498,560
6Dec 25, 2025 09:5128,85340,688,20316,735,00002,490,368
7Dec 25, 2025 09:4828,90039,022,72316,762,00002,510,848
8Dec 25, 2025 09:5029,14844,976,47516,906,00002,334,720
9Dec 25, 2025 11:2629,17130,751,09416,919,00002,322,432
10Dec 25, 2025 12:5429,21735,479,18415,950,000996,0002,498,560
11Dec 25, 2025 12:0629,31041,092,67416,000,0001,000,0002,314,240
12Dec 25, 2025 09:5129,40939,884,93316,054,0001,003,0002,535,424
13Dec 25, 2025 12:5429,51241,725,40816,111,0001,006,0002,322,432
14Dec 25, 2025 09:4929,51740,099,44416,113,0001,007,0002,486,272
15Dec 25, 2025 12:0729,57642,206,59915,136,0002,018,0002,510,848
16Dec 25, 2025 09:4929,61238,555,73216,165,0001,010,0002,498,560
17Dec 25, 2025 13:0129,72939,560,72817,243,00002,482,176
18Dec 25, 2025 09:5129,76636,568,77917,264,00002,478,080
19Dec 25, 2025 09:5029,85345,917,43117,315,00002,293,760
20Dec 25, 2025 09:5029,87133,695,82216,306,0001,019,0002,519,040
21Dec 25, 2025 11:2629,87137,546,28917,325,00002,449,408
22Dec 25, 2025 12:5429,96735,802,77717,381,00002,330,624
23Dec 25, 2025 12:5430,06737,318,22516,414,0001,025,0002,301,952
24Dec 25, 2025 09:4830,13434,092,06017,478,00002,494,464
25Dec 25, 2025 12:1030,14838,794,85717,486,00002,457,600
26Dec 25, 2025 09:5230,15240,053,73617,488,00002,506,752
27Dec 25, 2025 13:0130,25338,170,94917,547,00002,506,752
28Dec 25, 2025 09:4930,26745,419,34617,555,00002,301,952
29Dec 25, 2025 12:1030,32140,332,79317,586,00002,498,560
30Dec 25, 2025 11:2430,32843,501,04517,590,00002,531,328
31Dec 25, 2025 09:5330,33441,286,62717,594,00002,318,336
32Dec 25, 2025 09:4930,35037,496,31017,603,00002,457,600
33Dec 25, 2025 12:0630,37644,708,10416,582,0001,036,0002,482,176
34Dec 25, 2025 09:5030,38137,502,99316,585,0001,036,0002,318,336
35Dec 25, 2025 09:5130,38340,201,20516,586,0001,036,0002,314,240
36Dec 25, 2025 12:0730,44742,436,60317,659,00002,461,696
37Dec 25, 2025 09:5230,45545,015,43117,664,00002,265,088
38Dec 25, 2025 11:2730,48442,975,63017,681,00002,424,832
39Dec 25, 2025 12:5430,50944,727,14716,655,0001,040,0002,322,432
40Dec 25, 2025 13:0130,50944,771,83617,695,00002,478,080
41Dec 25, 2025 09:4830,57139,787,31216,688,0001,043,0002,371,584
42Dec 25, 2025 09:5030,58142,615,36716,694,0001,043,0002,478,080
43Dec 25, 2025 09:5130,59845,001,27817,747,00002,416,640
44Dec 25, 2025 09:5130,62643,418,57017,763,00002,310,144
45Dec 25, 2025 12:5430,86643,176,84416,908,000994,0002,502,656
46Dec 25, 2025 12:5430,86744,784,85016,909,000994,0002,285,568
47Dec 25, 2025 11:2430,87444,743,46817,907,00002,506,752
48Dec 25, 2025 09:4930,87647,932,63816,914,000994,0002,310,144
49Dec 25, 2025 09:4930,87945,410,71516,915,000995,0002,502,656
50Dec 25, 2025 12:0630,89130,724,74216,922,000995,0002,453,504
51Dec 25, 2025 12:0630,89542,818,49116,924,000995,0002,301,952
52Dec 25, 2025 09:5130,91045,329,80216,932,000996,0002,269,184
53Dec 25, 2025 12:0630,94746,927,78515,955,0001,994,0002,314,240
54Dec 25, 2025 09:5231,01238,643,07715,989,0001,998,0002,469,888
55Dec 25, 2025 09:5131,04538,915,20417,006,0001,000,0002,469,888
56Dec 25, 2025 11:2731,12435,484,93217,050,0001,002,0002,416,640
57Dec 25, 2025 12:5431,24140,883,79517,114,0001,006,0002,441,216
58Dec 25, 2025 11:2631,25341,029,39317,120,0001,007,0002,510,848
59Dec 25, 2025 09:5131,46647,212,93917,237,0001,013,0002,420,736
60Dec 25, 2025 12:0731,50538,492,78417,258,0001,015,0002,473,984
61Dec 25, 2025 12:0631,54840,319,72318,298,00002,506,752
62Dec 25, 2025 12:1031,55042,700,33617,283,0001,016,0002,482,176
63Dec 25, 2025 12:5431,66238,396,80718,364,00002,424,832
64Dec 25, 2025 09:5331,76253,451,62018,422,00002,424,832
65Dec 25, 2025 11:2731,88643,546,37918,494,00002,433,024
66Dec 25, 2025 09:5031,90543,452,91918,505,00002,404,352