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 23, 2025 23:26HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:57Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:5702,759,478,2462,750,303,0001,000,00016,384No answer to stdout. Died or killed?
2Dec 23, 2025 23:2129,72433,349,50517,240,00002,519,040
3Dec 23, 2025 23:2129,97441,198,57117,385,00002,297,856
4Dec 23, 2025 23:1430,16432,188,69717,495,00002,347,008
5Dec 23, 2025 23:1530,31442,656,79617,582,00002,490,368
6Dec 23, 2025 23:1530,39535,984,20116,592,0001,037,0002,306,048
7Dec 23, 2025 23:2030,42146,384,11417,644,00002,342,912
8Dec 23, 2025 23:2130,44133,634,26217,656,00002,326,528
9Dec 23, 2025 23:1730,47645,667,64317,676,00002,510,848
10Dec 23, 2025 23:2730,50050,226,67817,690,00002,510,848
11Dec 23, 2025 23:2730,50343,501,21517,692,00002,314,240
12Dec 23, 2025 23:2030,50740,885,85417,694,00002,326,528
13Dec 23, 2025 23:1530,60936,836,47417,753,00002,498,560
14Dec 23, 2025 23:2030,85947,217,88415,910,0001,988,0002,506,752
15Dec 23, 2025 23:1430,86643,534,42016,908,000994,0002,506,752
16Dec 23, 2025 23:2030,86639,243,11415,913,0001,989,0002,301,952
17Dec 23, 2025 23:2130,87445,652,39415,918,0001,989,0002,523,136
18Dec 23, 2025 23:1330,87651,611,70915,919,0001,989,0002,306,048
19Dec 23, 2025 23:1430,87647,623,52516,914,000994,0002,424,832
20Dec 23, 2025 23:1630,88447,944,64815,923,0001,990,0002,310,144
21Dec 23, 2025 23:1330,90744,239,22616,931,000995,0002,326,528
22Dec 23, 2025 23:2630,93631,884,35015,950,0001,993,0002,506,752
23Dec 23, 2025 23:1530,96937,457,39415,967,0001,995,0002,473,984
24Dec 23, 2025 23:1731,03840,904,96817,002,0001,000,0002,293,760
25Dec 23, 2025 23:1531,04034,701,07617,003,0001,000,0002,297,856
26Dec 23, 2025 23:1431,05339,769,54316,010,0002,001,0002,453,504
27Dec 23, 2025 23:1431,06242,773,48216,015,0002,001,0002,510,848
28Dec 23, 2025 23:2231,10243,071,00216,035,0002,004,0002,486,272
29Dec 23, 2025 23:1331,15539,521,80017,067,0001,003,0002,310,144
30Dec 23, 2025 23:1331,16939,539,60817,074,0001,004,0002,293,760
31Dec 23, 2025 23:1631,17645,280,56416,073,0002,009,0002,281,472
32Dec 23, 2025 23:1431,27641,928,57817,133,0001,007,0002,400,256
33Dec 23, 2025 23:1531,29041,757,44117,140,0001,008,0002,519,040
34Dec 23, 2025 23:1531,29547,317,46717,143,0001,008,0002,490,368
35Dec 23, 2025 23:1431,30542,613,17617,149,0001,008,0002,310,144
36Dec 23, 2025 23:2131,32238,841,91116,149,0002,018,0002,412,544
37Dec 23, 2025 23:2231,32238,860,56716,149,0002,018,0002,498,560
38Dec 23, 2025 23:1531,37237,974,47216,175,0002,021,0002,293,760
39Dec 23, 2025 23:1731,44039,552,05316,209,0002,026,0002,461,696
40Dec 23, 2025 23:1531,44545,204,26317,225,0001,013,0002,314,240
41Dec 23, 2025 23:1431,50939,791,92618,275,00002,510,848
42Dec 23, 2025 23:1531,51042,711,22317,261,0001,015,0002,310,144
43Dec 23, 2025 23:1531,51047,808,70917,261,0001,015,0002,412,544
44Dec 23, 2025 23:2231,53450,310,01617,274,0001,016,0002,494,464
45Dec 23, 2025 23:1331,58438,341,35718,319,00002,498,560
46Dec 23, 2025 23:2031,61242,934,69917,317,0001,018,0002,289,664
47Dec 23, 2025 23:1431,62447,644,66017,323,0001,019,0002,285,568
48Dec 23, 2025 23:1431,63844,005,98818,350,00002,273,280
49Dec 23, 2025 23:2731,68642,561,69617,357,0001,021,0002,490,368
50Dec 23, 2025 23:2031,69132,687,97618,381,00002,510,848
51Dec 23, 2025 23:1631,75946,024,33417,397,0001,023,0002,465,792
52Dec 23, 2025 23:2031,77840,894,11018,431,00002,482,176
53Dec 23, 2025 23:2031,78345,239,67618,434,00002,498,560
54Dec 23, 2025 23:1431,81940,424,35817,430,0001,025,0002,351,104
55Dec 23, 2025 23:1631,84138,300,20017,442,0001,026,0002,465,792
56Dec 23, 2025 23:1431,84737,995,74718,471,00002,457,600
57Dec 23, 2025 23:1331,87238,335,59718,486,00002,449,408
58Dec 23, 2025 23:2131,87939,663,36118,490,00002,285,568
59Dec 23, 2025 23:1431,88138,733,43918,491,00002,465,792
60Dec 23, 2025 23:2131,89135,708,00417,470,0001,027,0002,355,200
61Dec 23, 2025 23:1631,90039,303,83818,502,00002,519,040
62Dec 23, 2025 23:1631,90248,728,22318,503,00002,322,432
63Dec 23, 2025 23:2631,92134,789,89117,486,0001,028,0002,527,232
64Dec 23, 2025 23:1431,93839,758,77216,466,0002,058,0002,527,232
65Dec 23, 2025 23:1531,98440,658,58017,521,0001,030,0002,535,424
66Dec 23, 2025 23:2032,01239,148,19517,536,0001,031,0002,498,560
67Dec 23, 2025 23:2632,02135,113,24918,572,00002,318,336
68Dec 23, 2025 23:1532,10044,241,48617,584,0001,034,0002,510,848
69Dec 23, 2025 23:1532,26246,889,49517,673,0001,039,0002,441,216
70Dec 23, 2025 23:1532,27831,645,32418,721,00002,494,464
71Dec 23, 2025 23:2132,58644,933,29916,911,0001,989,0002,437,120
72Dec 23, 2025 23:2132,61746,592,10016,927,0001,991,0002,441,216
73Dec 23, 2025 23:1432,63641,044,34416,937,0001,992,0002,502,656