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 19:13HattonuriHattonuriScore: 31,134Success
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 19:1429,37146,609,80116,033,0001,002,0002,351,104
2Dec 23, 2025 19:5229,56240,518,33015,129,0002,017,0002,338,816
3Dec 23, 2025 19:1429,80745,559,17217,288,00002,523,136
4Dec 23, 2025 19:0829,84136,636,32115,272,0002,036,0002,469,888
5Dec 23, 2025 19:3529,90737,239,73317,346,00002,531,328
6Dec 23, 2025 19:1429,92937,169,45117,359,00002,531,328
7Dec 23, 2025 19:1330,11233,243,55717,465,00002,322,432
8Dec 23, 2025 19:1330,15237,642,19417,488,00002,510,848
9Dec 23, 2025 19:0830,15336,053,38116,461,0001,028,0002,519,040
10Dec 23, 2025 19:0730,20038,205,58516,486,0001,030,0002,306,048
11Dec 23, 2025 19:3730,23141,308,07417,534,00002,326,528
12Dec 23, 2025 19:5230,37138,841,74217,615,00002,269,184
13Dec 23, 2025 19:5230,39541,739,26716,592,0001,037,0002,338,816
14Dec 23, 2025 19:0830,40536,188,46217,635,00002,486,272
15Dec 23, 2025 19:3530,43144,710,49917,650,00002,326,528
16Dec 23, 2025 19:1330,43435,893,52716,614,0001,038,0002,510,848
17Dec 23, 2025 19:1330,45040,870,02316,623,0001,038,0002,502,656
18Dec 23, 2025 19:0830,53846,122,74716,671,0001,041,0002,519,040
19Dec 23, 2025 19:1430,64145,687,98217,772,00002,338,816
20Dec 23, 2025 19:1430,86033,864,61615,911,0001,988,0002,433,024
21Dec 23, 2025 19:5230,86631,429,55016,908,000994,0002,293,760
22Dec 23, 2025 19:0730,86945,692,20815,915,0001,989,0002,510,848
23Dec 23, 2025 19:1430,87442,838,96116,913,000994,0002,322,432
24Dec 23, 2025 19:1330,87648,423,03015,919,0001,989,0002,314,240
25Dec 23, 2025 19:1430,88141,979,79716,916,000995,0002,473,984
26Dec 23, 2025 19:1330,88444,102,43616,918,000995,0002,453,504
27Dec 23, 2025 19:5330,88644,895,63816,919,000995,0002,318,336
28Dec 23, 2025 19:0630,89342,573,92616,923,000995,0002,297,856
29Dec 23, 2025 19:1330,92141,969,18816,938,000996,0002,523,136
30Dec 23, 2025 19:0731,06039,922,86017,015,0001,000,0002,498,560
31Dec 23, 2025 19:0931,06243,052,73017,016,0001,000,0002,322,432
32Dec 23, 2025 19:0931,06933,929,84517,019,0001,001,0002,457,600
33Dec 23, 2025 19:5231,08640,396,63717,029,0001,001,0002,510,848
34Dec 23, 2025 19:0831,09839,684,41716,033,0002,004,0002,396,160
35Dec 23, 2025 19:1431,10247,042,07117,037,0001,002,0002,310,144
36Dec 23, 2025 19:0931,10238,189,65817,037,0001,002,0002,473,984
37Dec 23, 2025 19:0931,10742,629,86217,040,0001,002,0002,449,408
38Dec 23, 2025 19:0831,11241,916,49017,043,0001,002,0002,424,832
39Dec 23, 2025 19:0731,11435,840,93317,044,0001,002,0002,285,568
40Dec 23, 2025 19:1431,11650,582,51717,045,0001,002,0002,486,272
41Dec 23, 2025 19:0931,13437,954,28917,055,0001,003,0002,506,752
42Dec 23, 2025 19:5331,15342,699,88117,066,0001,003,0002,293,760
43Dec 23, 2025 19:3731,22640,873,07417,105,0001,006,0002,523,136
44Dec 23, 2025 19:0831,24839,658,97317,118,0001,006,0002,486,272
45Dec 23, 2025 19:0731,25745,226,28917,122,0001,007,0002,510,848
46Dec 23, 2025 19:5331,26943,668,23816,121,0002,015,0002,486,272
47Dec 23, 2025 19:0631,28335,766,31616,128,0002,016,0002,363,392
48Dec 23, 2025 19:0931,32942,585,56616,152,0002,019,0002,478,080
49Dec 23, 2025 19:3731,37241,706,77717,186,0001,010,0002,510,848
50Dec 23, 2025 19:0931,39139,246,86916,184,0002,023,0002,506,752
51Dec 23, 2025 19:1331,39843,279,26917,200,0001,011,0002,293,760
52Dec 23, 2025 19:0731,44731,436,73516,213,0002,026,0002,416,640
53Dec 23, 2025 19:3731,47139,952,82317,239,0001,014,0002,510,848
54Dec 23, 2025 19:5231,52135,300,61817,267,0001,015,0002,478,080
55Dec 23, 2025 19:1331,53344,708,66718,289,00002,490,368
56Dec 23, 2025 19:3731,54741,769,67417,281,0001,016,0002,408,448
57Dec 23, 2025 19:0931,57440,525,12218,313,00002,379,776
58Dec 23, 2025 19:1331,59142,580,25318,323,00002,457,600
59Dec 23, 2025 19:5231,60241,499,78118,329,00002,510,848
60Dec 23, 2025 19:5231,60340,922,39918,330,00002,445,312
61Dec 23, 2025 19:1431,64739,709,08518,355,00002,289,664
62Dec 23, 2025 19:1331,70251,218,07818,387,00002,486,272
63Dec 23, 2025 19:3531,73441,035,99518,406,00002,523,136
64Dec 23, 2025 19:1331,76040,496,93917,398,0001,023,0002,457,600
65Dec 23, 2025 19:0731,78438,987,00818,435,00002,265,088
66Dec 23, 2025 19:5231,78643,666,69518,436,00002,510,848
67Dec 23, 2025 19:0931,80043,307,84917,420,0001,024,0002,510,848
68Dec 23, 2025 19:0731,80040,066,73118,444,00002,535,424
69Dec 23, 2025 19:0831,80539,508,66417,423,0001,024,0002,408,448
70Dec 23, 2025 19:3731,81744,672,55317,429,0001,025,0002,490,368
71Dec 23, 2025 19:0731,86638,494,68217,456,0001,026,0002,469,888
72Dec 23, 2025 19:0731,89739,852,82718,500,00002,510,848
73Dec 23, 2025 19:1431,94541,204,80418,528,00002,469,888
74Dec 23, 2025 19:1331,96243,685,78817,509,0001,029,0002,428,928
75Dec 23, 2025 19:0831,98139,262,86118,549,00002,510,848
76Dec 23, 2025 19:0632,12439,542,53018,632,00002,502,656
77Dec 23, 2025 19:0732,19843,305,62517,638,0001,037,0002,424,832
78Dec 23, 2025 19:1332,31937,811,97617,704,0001,041,0002,502,656
79Dec 23, 2025 19:0732,32847,794,75417,709,0001,041,0002,437,120
80Dec 23, 2025 19:1432,50936,715,04616,871,0001,984,0002,445,312
81Dec 23, 2025 19:1332,59048,646,98616,913,0001,989,0002,465,792