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 listOct 17, 2024 11:45@drytecc@dryteccError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 17, 2024 11:4500000Error: expected "2209951 ", got "2207677 "
2Oct 17, 2024 11:4600000Error: expected "588908 ", got "587042 "
3Oct 17, 2024 11:4600000Error: expected "1491256 ", got "2009402 "
4Oct 17, 2024 11:4600000Error: expected "921873 ", got "946561 "
5Oct 17, 2024 11:4600000Error: expected "972185 ", got "1080940 "
6Oct 17, 2024 11:4600000Error: expected "1798475 ", got "2333978 "
7Oct 17, 2024 11:4600000Error: expected "844980 ", got "847401 "
8Oct 17, 2024 11:4600000Error: expected "1748871 ", got "1812198 "
9Oct 17, 2024 11:4600000Error: expected "963464 ", got "999011 "
10Oct 17, 2024 11:4600000Error: expected "1432570 ", got "1622849 "
11Oct 17, 2024 11:4600000Error: expected "747875 ", got "747942 "
12Oct 17, 2024 11:4600000Error: expected "1422707 ", got "1423037 "
13Oct 17, 2024 11:4600000Error: expected "1502169 ", got "1602080 "
14Oct 17, 2024 11:4600000Error: expected "1501285 ", got "1498223 "
15Oct 17, 2024 11:4600000Error: expected "2761877 ", got "2762192 "
16Oct 17, 2024 11:4600000Error: expected "883796 ", got "1145565 "
17Oct 17, 2024 11:4700000Error: expected "1744873 ", got "1766569 "
18Oct 17, 2024 11:4700000Error: expected "1408594 ", got "1438647 "
19Oct 17, 2024 11:4700000Error: expected "751335 ", got "752624 "
20Oct 17, 2024 11:4700000Error: expected "1258584 ", got "1260262 "
21Oct 17, 2024 11:4700000Error: expected "1050431 ", got "5690651 "
22Oct 17, 2024 11:4700000Error: expected "650841 ", got "660069 "
23Oct 17, 2024 11:4700000Error: expected "2266407 ", got "2397443 "
24Oct 17, 2024 11:4700000Error: expected "1798212 ", got "1797943 "
25Oct 17, 2024 11:4700000Error: expected "1800315 ", got "1869691 "
26Oct 17, 2024 11:4700000Error: expected "781556 ", got "799923 "
27Oct 17, 2024 11:4700000Error: expected "822956 ", got "862349 "
28Oct 17, 2024 11:4700000Error: expected "1255805 ", got "1317337 "
29Oct 17, 2024 11:4700000Error: expected "727020 ", got "796155 "
30Oct 17, 2024 11:4700000Error: expected "771754 ", got "861724 "
31Oct 17, 2024 11:4700000Error: expected "788646 ", got "803957 "
32Oct 17, 2024 11:4700000Error: expected "2102483 ", got "2134423 "
33Oct 17, 2024 11:4700000Error: expected "1803407 ", got "1803787 "
34Oct 17, 2024 11:4700000Error: expected "3640573 ", got "3636127 "
35Oct 17, 2024 11:4700000Error: expected "2113357 ", got "2175135 "
36Oct 17, 2024 11:4700000Error: expected "1489025 ", got "1490483 "
37Oct 17, 2024 11:4700000Error: expected "1423326 ", got "1423634 "
38Oct 17, 2024 11:4700000Error: expected "2694243 ", got "3041745 "
39Oct 17, 2024 11:4700000Error: expected "681616 ", got "683352 "
40Oct 17, 2024 11:4700000Error: expected "1087149 ", got "1088139 "
41Oct 17, 2024 11:4800000Error: expected "2612473 ", got "3016414 "
42Oct 17, 2024 11:4800000Error: expected "1455956 ", got "1507306 "
43Oct 17, 2024 11:4800000Error: expected "702653 ", got "923988 "
44Oct 17, 2024 11:4800000Error: expected "2130640 ", got "2136810 "
45Oct 17, 2024 11:4800000Error: expected "1022065 ", got "1082666 "
46Oct 17, 2024 11:4800000Error: expected "1328592 ", got "3916502 "
47Oct 17, 2024 11:4800000Error: expected "1241791 ", got "1726189 "
48Oct 17, 2024 11:4800000Error: expected "624806 ", got "739278 "
49Oct 17, 2024 11:4800000Error: expected "1525329 ", got "1951322 "
50Oct 17, 2024 11:4800000Error: expected "748228 ", got "749559 "
51Oct 17, 2024 11:4800000Error: expected "1002122 ", got "1090370 "
52Oct 17, 2024 11:4800000Error: expected "3261313 ", got "3265455 "
53Oct 17, 2024 11:4800000Error: expected "1192358 ", got "1288519 "
54Oct 17, 2024 11:4800000Error: expected "1784933 ", got "1790786 "
55Oct 17, 2024 11:4800000Error: No answer to stdout. Died or killed?
56Oct 17, 2024 11:4800000Error: expected "922165 ", got "938572 "
57Oct 17, 2024 11:4800000Error: expected "821253 ", got "1079421 "
58Oct 17, 2024 11:4800000Error: expected "809596 ", got "845990 "
59Oct 17, 2024 11:4800000Error: No answer to stdout. Died or killed?
60Oct 17, 2024 11:4800000Error: expected "1891616 ", got "1894406 "
61Oct 17, 2024 11:4800000Error: expected "1590864 ", got "1687214 "
62Oct 17, 2024 11:4900000Error: expected "1533110 ", got "1592701 "
63Oct 17, 2024 11:4900000Error: expected "2228851 ", got "2245365 "
64Oct 17, 2024 11:4900000Error: expected "993062 ", got "992111 "
65Oct 17, 2024 11:4900000Error: expected "781191 ", got "814361 "
66Oct 17, 2024 11:4900000Error: expected "1228220 ", got "1230172 "
67Oct 17, 2024 11:4900000Error: expected "2103373 ", got "2170535 "
68Oct 17, 2024 11:4900000Error: expected "931634 ", got "1126649 "
69Oct 17, 2024 11:4900000Error: expected "2361764 ", got "2441116 "
70Oct 17, 2024 11:4900000Error: expected "2714005 ", got "2714955 "
71Oct 17, 2024 11:4900000Error: expected "1252043 ", got "1266301 "
72Oct 17, 2024 11:4900000Error: expected "1932575 ", got "1939160 "
73Oct 17, 2024 11:4900000Error: expected "2421508 ", got "2693561 "
74Oct 17, 2024 11:4900000Error: expected "829687 ", got "829631 "
75Oct 17, 2024 11:4900000Error: expected "1072847 ", got "1101510 "
76Oct 17, 2024 11:4900000Error: expected "1308056 ", got "1307912 "
77Oct 17, 2024 11:4900000Error: No answer to stdout. Died or killed?
78Oct 17, 2024 11:4900000Error: expected "2054340 ", got "2103389 "
79Oct 17, 2024 11:4900000Error: expected "974246 ", got "1072487 "
80Oct 17, 2024 11:4900000Error: expected "2253068 ", got "2297025 "
81Oct 17, 2024 11:4900000Error: expected "1757469 ", got "1757772 "
82Oct 17, 2024 11:4900000Error: expected "1416166 ", got "1438206 "
83Oct 17, 2024 11:4900000Error: expected "866694 ", got "1400252 "
84Oct 17, 2024 11:5000000Error: expected "1344496 ", got "1415631 "
85Oct 17, 2024 12:3400000Error: expected "2709546 ", got "2725182 "
86Oct 17, 2024 12:3400000Error: expected "1976751 ", got "1984145 "
87Oct 17, 2024 12:3400000Error: expected "1427917 ", got "1432296 "
88Oct 17, 2024 11:4740,09549,511,53721,233,0002,022,0002,514,944
89Oct 17, 2024 12:3440,20545,099,18020,278,0003,041,0002,502,656
90Oct 17, 2024 11:4940,29548,741,43720,323,0003,048,0002,465,792
91Oct 17, 2024 11:4740,29847,585,44521,341,0002,032,0002,412,544
92Oct 17, 2024 11:4840,49750,634,83119,403,0004,085,0002,289,664
93Oct 17, 2024 12:3440,58454,592,62222,516,0001,023,0002,523,136
94Oct 17, 2024 11:4940,59550,728,10020,474,0003,071,0002,465,792
95Oct 17, 2024 11:4940,62450,558,14120,489,0003,073,0002,314,240
96Oct 17, 2024 11:4740,63649,490,18421,520,0002,049,0002,469,888
97Oct 17, 2024 11:4740,65049,086,01222,552,0001,025,0002,424,832
98Oct 17, 2024 11:4740,66044,487,89522,558,0001,025,0002,469,888
99Oct 17, 2024 12:3440,67652,439,18221,541,0002,051,0002,510,848
100Oct 17, 2024 11:4740,75253,274,04620,553,0003,083,0002,461,696
101Oct 17, 2024 11:4740,78855,006,04821,600,0002,057,0002,469,888
102Oct 17, 2024 11:4940,80749,940,96819,552,0004,116,0002,424,832
103Oct 17, 2024 11:4840,92847,863,60619,610,0004,128,0002,273,280
104Oct 17, 2024 11:4940,95050,394,40721,686,0002,065,0002,465,792
105Oct 17, 2024 11:4941,12447,832,82421,865,0001,987,0002,465,792
106Oct 17, 2024 12:3441,15952,122,51421,883,0001,989,0002,392,064
107Oct 17, 2024 11:4841,16748,330,60521,888,0001,989,0002,461,696
108Oct 17, 2024 11:4841,17854,001,96921,893,0001,990,0002,416,640
109Oct 17, 2024 11:4941,17848,865,04621,893,0001,990,0002,379,776
110Oct 17, 2024 11:4741,20052,028,39618,918,0004,978,0002,514,944
111Oct 17, 2024 11:4741,20957,059,39722,906,000995,0002,347,008
112Oct 17, 2024 11:4841,22649,628,62022,915,000996,0002,486,272
113Oct 17, 2024 11:4841,24153,700,59819,934,0003,986,0002,420,736
114Oct 17, 2024 11:4841,24353,787,90521,928,0001,993,0002,428,928
115Oct 17, 2024 11:4741,25048,444,91119,938,0003,987,0002,461,696
116Oct 17, 2024 11:4941,26458,049,81320,942,0002,991,0002,473,984
117Oct 17, 2024 11:4741,38649,778,29222,004,0002,000,0002,469,888
118Oct 17, 2024 11:4841,39751,571,01422,010,0002,000,0002,473,984
119Oct 17, 2024 12:3441,41751,090,41721,020,0003,002,0002,486,272
120Oct 17, 2024 11:4941,49052,014,32822,059,0002,005,0002,310,144
121Oct 17, 2024 11:4841,62153,033,28622,129,0002,011,0002,514,944
122Oct 17, 2024 11:4741,74762,774,88921,187,0003,026,0002,473,984
123Oct 17, 2024 12:3446,86453,644,53124,161,0003,020,0002,473,984