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:26HattonuriHattonuriScore: 31,372Success
Source Code

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

Challenge History

No challenges yet.

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