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 16, 2024 13:55@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
1Apr 24, 2021 08:4500000no run stats recorded
2Oct 16, 2024 13:5500000Error: expected "967881 ", got "977710 "
3Oct 16, 2024 13:5500000Error: expected "1701986 ", got "1723274 "
4Oct 16, 2024 13:5500000Error: expected "1447729 ", got "1447688 "
5Oct 16, 2024 13:5500000Error: expected "1025116 ", got "1043001 "
6Oct 16, 2024 13:5500000Error: expected "1412724 ", got "1411104 "
7Oct 16, 2024 13:5500000Error: expected "1236100 ", got "1244494 "
8Oct 16, 2024 13:5500000Error: expected "1622293 ", got "1624077 "
9Oct 16, 2024 13:5500000Error: expected "3098966 ", got "3099184 "
10Oct 16, 2024 13:5500000Error: expected "687382 ", got "1656588 "
11Oct 16, 2024 13:5500000Error: expected "1866349 ", got "1866259 "
12Oct 16, 2024 13:5500000Error: expected "543353 ", got "542881 "
13Oct 16, 2024 13:5500000Error: expected "1609754 ", got "1683325 "
14Oct 16, 2024 13:5500000Error: expected "1163485 ", got "1183134 "
15Oct 16, 2024 13:5500000Error: expected "2504223 ", got "2517539 "
16Oct 16, 2024 13:5500000Error: expected "1606571 ", got "1607009 "
17Oct 16, 2024 13:5500000Error: expected "1735320 ", got "1735054 "
18Oct 16, 2024 13:5500000Error: expected "1448489 ", got "1448698 "
19Oct 16, 2024 13:5500000Error: expected "1421683 ", got "1425826 "
20Oct 16, 2024 13:5500000Error: expected "2295815 ", got "2295657 "
21Oct 16, 2024 13:5500000Error: expected "946662 ", got "946728 "
22Oct 16, 2024 13:5500000Error: expected "1271629 ", got "1273611 "
23Oct 16, 2024 13:5500000Error: expected "1970588 ", got "1964820 "
24Oct 16, 2024 13:5500000Error: expected "1920781 ", got "1929726 "
25Oct 16, 2024 13:5500000Error: expected "1909475 ", got "1925101 "
26Oct 16, 2024 13:5600000Error: expected "2470726 ", got "2470778 "
27Oct 16, 2024 13:5600000Error: expected "1483586 ", got "1483696 "
28Oct 16, 2024 13:5600000Error: expected "1811432 ", got "1814805 "
29Oct 16, 2024 13:5600000Error: expected "771450 ", got "774247 "
30Oct 16, 2024 13:5600000Error: expected "891639 ", got "924763 "
31Oct 16, 2024 13:5600000Error: expected "1202193 ", got "1203825 "
32Oct 16, 2024 13:5800000Error: expected "897200 ", got "901347 "
33Oct 16, 2024 13:5800000Error: expected "2243128 ", got "2240656 "
34Oct 16, 2024 13:5800000Error: expected "722068 ", got "728246 "
35Oct 16, 2024 13:5800000Error: expected "2539729 ", got "2539017 "
36Oct 16, 2024 13:5800000Error: expected "712915 ", got "713055 "
37Oct 16, 2024 13:5800000Error: expected "1740136 ", got "1755835 "
38Oct 16, 2024 13:5800000Error: expected "1471173 ", got "1466184 "
39Oct 16, 2024 13:5800000Error: expected "881689 ", got "921669 "
40Oct 16, 2024 13:5800000Error: expected "2866786 ", got "2886666 "
41Oct 16, 2024 13:5800000Error: expected "1299290 ", got "1320384 "
42Oct 16, 2024 13:5800000Error: expected "1159697 ", got "1162071 "
43Oct 16, 2024 13:5800000Error: expected "1218310 ", got "1218293 "
44Oct 16, 2024 13:5800000Error: expected "720026 ", got "720147 "
45Oct 16, 2024 13:5800000Error: expected "1154842 ", got "1166694 "
46Oct 16, 2024 13:5800000Error: expected "994151 ", got "992207 "
47Oct 16, 2024 13:5800000Error: expected "1650758 ", got "2325765 "
48Oct 16, 2024 13:5900000Error: expected "2144472 ", got "2161987 "
49Oct 16, 2024 13:5900000Error: expected "2396843 ", got "2396777 "
50Oct 16, 2024 13:5900000Error: expected "1097556 ", got "1098042 "
51Oct 16, 2024 13:5900000Error: expected "1311638 ", got "1317560 "
52Oct 16, 2024 13:5900000Error: expected "2157678 ", got "2164257 "
53Oct 16, 2024 13:5900000Error: expected "929553 ", got "1074196 "
54Oct 16, 2024 13:5900000Error: expected "775746 ", got "771698 "
55Oct 16, 2024 13:5900000Error: expected "1278423 ", got "1277075 "
56Oct 16, 2024 13:5900000Error: expected "648758 ", got "650127 "
57Oct 16, 2024 13:5900000Error: expected "1341796 ", got "1341886 "
58Oct 16, 2024 13:5900000Error: expected "938731 ", got "964912 "
59Oct 16, 2024 13:5900000Error: expected "1713276 ", got "1805878 "
60Oct 16, 2024 13:5900000Error: expected "2902897 ", got "2904305 "
61Oct 16, 2024 14:0200000Error: expected "1076851 ", got "1087950 "
62Oct 16, 2024 14:0200000Error: expected "1478140 ", got "1517353 "
63Oct 16, 2024 14:0200000Error: expected "1003655 ", got "1020591 "
64Oct 16, 2024 14:0200000Error: expected "1052614 ", got "1095706 "
65Oct 16, 2024 14:0200000Error: expected "589441 ", got "589144 "
66Oct 16, 2024 14:0200000Error: expected "2473715 ", got "2502779 "
67Oct 16, 2024 14:0200000Error: expected "1387609 ", got "1531508 "
68Oct 16, 2024 14:0200000Error: expected "2239008 ", got "2287219 "
69Oct 16, 2024 14:0200000Error: expected "2054403 ", got "2055531 "
70Oct 16, 2024 14:0200000Error: expected "1678155 ", got "1676414 "
71Oct 16, 2024 14:0200000Error: expected "973622 ", got "973186 "
72Oct 16, 2024 14:0200000Error: expected "3056089 ", got "3061150 "
73Oct 16, 2024 14:0200000Error: expected "2386233 ", got "2386783 "
74Oct 16, 2024 14:0200000Error: expected "693104 ", got "691446 "
75Oct 16, 2024 14:0200000Error: expected "1672531 ", got "1671683 "
76Oct 16, 2024 14:0200000Error: expected "668366 ", got "668136 "
77Oct 16, 2024 14:0300000Error: expected "873572 ", got "958403 "
78Oct 16, 2024 14:0300000Error: expected "1544230 ", got "1646009 "
79Oct 16, 2024 14:0300000Error: expected "2720180 ", got "2827217 "
80Oct 16, 2024 14:0300000Error: expected "949181 ", got "949055 "
81Oct 16, 2024 14:0300000Error: expected "2036264 ", got "2037029 "
82Oct 16, 2024 14:0300000Error: expected "1808538 ", got "1810766 "
83Oct 16, 2024 14:0300000Error: expected "880036 ", got "956797 "
84Oct 16, 2024 14:0300000Error: expected "2207661 ", got "2209144 "
85Oct 16, 2024 14:0300000Error: expected "1944072 ", got "1963306 "
86Oct 16, 2024 14:0300000Error: expected "2258140 ", got "2317696 "
87Oct 16, 2024 14:0300000Error: expected "1762263 ", got "1763097 "
88Oct 16, 2024 14:0300000Error: expected "2099902 ", got "2098058 "
89Oct 16, 2024 14:0800000Error: expected "1684418 ", got "1687526 "
90Oct 16, 2024 14:0800000Error: expected "1061045 ", got "1087258 "
91Oct 16, 2024 14:0800000Error: expected "1366005 ", got "1391261 "
92Oct 16, 2024 14:0800000Error: expected "1540053 ", got "1546200 "
93Oct 16, 2024 14:0800000Error: expected "1464126 ", got "1498311 "
94Oct 16, 2024 14:0800000Error: expected "2273729 ", got "2274605 "
95Oct 16, 2024 14:0800000Error: expected "887957 ", got "890323 "
96Oct 16, 2024 14:0800000Error: expected "1127118 ", got "1203034 "
97Oct 16, 2024 14:0800000Error: expected "1848558 ", got "1861686 "
98Oct 16, 2024 14:0800000Error: expected "928801 ", got "927817 "
99Oct 16, 2024 14:0800000Error: expected "2303697 ", got "2412083 "
100Oct 16, 2024 14:0800000Error: expected "1187523 ", got "1192166 "
101Oct 16, 2024 14:0800000Error: expected "1404928 ", got "1404454 "
102Oct 16, 2024 14:0800000Error: expected "2781226 ", got "3170290 "
103Oct 16, 2024 14:0800000Error: expected "2516453 ", got "2574763 "
104Oct 16, 2024 14:0800000Error: expected "1415705 ", got "1785228 "
105Oct 16, 2024 14:0800000Error: expected "780237 ", got "930137 "
106Oct 16, 2024 14:0800000Error: expected "1117042 ", got "1132238 "
107Oct 16, 2024 14:0800000Error: expected "689703 ", got "714799 "
108Oct 16, 2024 14:0800000Error: expected "3380512 ", got "3379645 "
109Oct 16, 2024 14:0800000Error: expected "1966533 ", got "1983019 "
110Oct 16, 2024 14:0900000Error: expected "1620381 ", got "1680831 "
111Oct 16, 2024 14:0900000Error: expected "1436332 ", got "1630616 "
112Oct 16, 2024 14:0900000Error: expected "1238562 ", got "1243785 "
113Oct 16, 2024 14:0900000Error: expected "1669631 ", got "1782353 "
114Oct 16, 2024 14:0900000Error: expected "1401499 ", got "1406899 "
115Oct 16, 2024 14:0900000Error: expected "2710288 ", got "2708457 "
116Oct 16, 2024 14:0900000Error: expected "734992 ", got "734501 "
117Oct 16, 2024 14:0900000Error: expected "1552759 ", got "1598993 "
118Oct 16, 2024 14:0900000Error: expected "878247 ", got "879687 "
119Oct 16, 2024 14:0900000Error: expected "1877670 ", got "1925341 "
120Oct 16, 2024 14:0900000Error: expected "883627 ", got "883789 "
121Oct 16, 2024 14:0900000Error: expected "1177727 ", got "1177705 "
122Oct 16, 2024 14:0900000Error: expected "840141 ", got "959714 "
123Oct 16, 2024 14:0900000Error: expected "1301391 ", got "1339082 "
124Oct 16, 2024 14:0900000Error: expected "982424 ", got "1126232 "
125Oct 16, 2024 14:0900000Error: expected "574801 ", got "576573 "
126Oct 16, 2024 14:0900000Error: expected "914422 ", got "912034 "
127Oct 16, 2024 14:0900000Error: expected "1412782 ", got "1407819 "
128Oct 16, 2024 14:0900000Error: expected "1201673 ", got "1204065 "
129Oct 16, 2024 14:0900000Error: expected "1528936 ", got "1530357 "
130Oct 16, 2024 14:0900000Error: expected "1329499 ", got "1349544 "
131Oct 16, 2024 14:0900000Error: expected "1761313 ", got "1761593 "
132Oct 16, 2024 14:1000000Error: expected "1097749 ", got "1103411 "
133Oct 16, 2024 14:1000000Error: expected "854911 ", got "867669 "
134Oct 16, 2024 14:1000000Error: expected "706136 ", got "776369 "
135Oct 16, 2024 14:1300000Error: expected "1690130 ", got "1689896 "
136Oct 16, 2024 14:1300000Error: expected "1761735 ", got "1762519 "
137Oct 16, 2024 14:1300000Error: expected "1094356 ", got "1093798 "
138Oct 16, 2024 14:1300000Error: expected "902049 ", got "902085 "
139Oct 16, 2024 14:1300000Error: expected "721250 ", got "719737 "
140Oct 16, 2024 14:1300000Error: expected "1257873 ", got "1263572 "
141Oct 16, 2024 14:1300000Error: expected "797095 ", got "825028 "
142Oct 16, 2024 14:0948,04054,919,00223,883,0003,980,0002,449,408
143Oct 16, 2024 14:0248,06756,939,93423,897,0003,982,0002,465,792
144Oct 16, 2024 14:0348,08453,995,76023,905,0003,984,0002,473,984
145Oct 16, 2024 14:0948,09356,960,01225,902,0001,992,0002,478,080
146Oct 16, 2024 14:0248,10957,862,09624,914,0002,989,0002,461,696
147Oct 16, 2024 14:0948,11462,758,52825,913,0001,993,0002,465,792
148Oct 16, 2024 14:1048,12155,248,40425,917,0001,993,0002,510,848
149Oct 16, 2024 14:0848,15267,063,83124,825,0003,103,0002,502,656
150Oct 16, 2024 14:0348,16050,752,04025,864,0002,069,0002,289,664
151Oct 16, 2024 14:0948,20348,916,75926,960,000998,0002,523,136
152Oct 16, 2024 14:0248,21451,356,24224,968,0002,996,0002,465,792
153Oct 16, 2024 14:0348,21451,232,95924,968,0002,996,0002,441,216
154Oct 16, 2024 14:1048,21755,990,92724,970,0002,996,0002,457,600
155Oct 16, 2024 14:0348,25056,060,64422,988,0004,997,0002,469,888
156Oct 16, 2024 14:0948,26650,956,00422,995,0004,999,0002,486,272
157Oct 16, 2024 14:0248,27654,973,22525,000,0003,000,0002,347,008
158Oct 16, 2024 14:0248,33451,682,88723,028,0005,006,0002,404,352
159Oct 16, 2024 14:0948,40754,456,58725,068,0003,008,0002,469,888
160Oct 16, 2024 14:1048,44750,477,00926,092,0002,007,0002,469,888
161Oct 16, 2024 14:0248,49350,276,04925,113,0003,013,0002,301,952
162Oct 16, 2024 14:0348,63451,255,09025,186,0003,022,0002,310,144
163Oct 16, 2024 14:0348,63653,338,13023,172,0005,037,0002,433,024
164Oct 16, 2024 14:0948,74754,171,65426,254,0002,019,0002,465,792
165Oct 16, 2024 14:0348,78854,530,63624,255,0004,042,0002,330,624
166Oct 16, 2024 14:0848,86051,696,59425,303,0003,036,0002,465,792
167Oct 16, 2024 14:0248,93157,108,32926,353,0002,027,0002,523,136
168Oct 16, 2024 14:0948,95560,749,14223,324,0005,070,0002,473,984
169Oct 16, 2024 14:0348,95950,239,61825,354,0003,042,0002,465,792
170Oct 16, 2024 14:0348,97151,237,29025,360,0003,043,0002,473,984
171Oct 16, 2024 14:0849,04152,632,54124,381,0004,063,0002,334,720
172Oct 16, 2024 14:0249,08656,575,88525,420,0003,050,0002,379,776
173Oct 16, 2024 14:0949,21055,951,07925,484,0003,058,0002,363,392
174Oct 16, 2024 14:0849,42260,727,48125,594,0003,071,0002,469,888
175Oct 16, 2024 14:0850,20754,599,74125,104,0004,016,0002,465,792
176Oct 16, 2024 14:0852,52159,346,20028,432,0002,030,0002,469,888
177Oct 16, 2024 14:0252,98660,572,83728,684,0002,048,0002,461,696