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 12:47@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 12:4700000Error: expected "2196328 ", got "2991784 "
2Oct 17, 2024 12:4700000Error: expected "1329314 ", got "1329416 "
3Oct 17, 2024 12:4700000Error: expected "1235598 ", got "1365197 "
4Oct 17, 2024 12:4700000Error: No answer to stdout. Died or killed?
5Oct 17, 2024 12:4700000Error: expected "886539 ", got "887297 "
6Oct 17, 2024 12:4700000Error: expected "576613 ", got "580249 "
7Oct 17, 2024 12:4700000Error: expected "1911529 ", got "1910688 "
8Oct 17, 2024 12:4700000Error: No answer to stdout. Died or killed?
9Oct 17, 2024 12:4700000Error: No answer to stdout. Died or killed?
10Oct 17, 2024 12:4800000Error: expected "1791765 ", got "1909086 "
11Oct 17, 2024 12:4800000Error: expected "741819 ", got "750423 "
12Oct 17, 2024 12:4800000Error: expected "1425314 ", got "1426214 "
13Oct 17, 2024 12:4800000Error: No answer to stdout. Died or killed?
14Oct 17, 2024 12:4800000Error: No answer to stdout. Died or killed?
15Oct 17, 2024 12:4800000Error: expected "1311586 ", got "1326135 "
16Oct 17, 2024 12:4800000Error: expected "1069652 ", got "1079208 "
17Oct 17, 2024 12:4900000Error: No answer to stdout. Died or killed?
18Oct 17, 2024 12:4900000Error: expected "1119590 ", got "1221149 "
19Oct 17, 2024 12:4900000Error: expected "1317253 ", got "1365828 "
20Oct 17, 2024 12:4900000Error: expected "1859704 ", got "2036293 "
21Oct 17, 2024 12:4900000Error: expected "1199362 ", got "1199422 "
22Oct 17, 2024 12:4900000Error: expected "1558720 ", got "1560825 "
23Oct 17, 2024 12:4900000Error: No answer to stdout. Died or killed?
24Oct 17, 2024 12:5000000Error: expected "1221929 ", got "1225077 "
25Oct 17, 2024 12:5000000Error: No answer to stdout. Died or killed?
26Oct 17, 2024 12:5000000Error: expected "1041366 ", got "1507739 "
27Oct 17, 2024 12:5000000Error: expected "4119330 ", got "4119726 "
28Oct 17, 2024 12:5000000Error: expected "700896 ", got "700304 "
29Oct 17, 2024 12:5000000Error: expected "3867735 ", got "4214252 "
30Oct 17, 2024 12:5000000Error: No answer to stdout. Died or killed?
31Oct 17, 2024 12:5000000Error: expected "1747393 ", got "1747431 "
32Oct 17, 2024 12:5000000Error: No answer to stdout. Died or killed?
33Oct 17, 2024 12:5000000Error: No answer to stdout. Died or killed?
34Oct 17, 2024 12:5000000Error: expected "1402482 ", got "1403185 "
35Oct 17, 2024 12:5000000Error: expected "1919698 ", got "1934877 "
36Oct 17, 2024 12:5000000Error: expected "920154 ", got "938396 "
37Oct 17, 2024 12:5000000Error: expected "1119050 ", got "1134157 "
38Oct 17, 2024 12:5100000Error: expected "1057410 ", got "1175771 "
39Oct 17, 2024 12:5100000Error: expected "1332280 ", got "1354918 "
40Oct 17, 2024 12:5100000Error: No answer to stdout. Died or killed?
41Oct 17, 2024 12:5100000Error: expected "2669645 ", got "2692505 "
42Oct 17, 2024 12:5100000Error: expected "2449496 ", got "2477758 "
43Oct 17, 2024 12:5100000Error: expected "1576997 ", got "1580623 "
44Oct 17, 2024 12:5100000Error: expected "647728 ", got "648406 "
45Oct 17, 2024 12:5100000Error: expected "667108 ", got "710445 "
46Oct 17, 2024 12:5100000Error: No answer to stdout. Died or killed?
47Oct 17, 2024 12:5100000Error: expected "1225185 ", got "1232228 "
48Oct 17, 2024 12:5100000Error: expected "1445570 ", got "1626638 "
49Oct 17, 2024 12:5100000Error: expected "1650953 ", got "1779881 "
50Oct 17, 2024 12:5100000Error: No answer to stdout. Died or killed?
51Oct 17, 2024 12:5100000Error: No answer to stdout. Died or killed?
52Oct 17, 2024 12:5100000Error: No answer to stdout. Died or killed?
53Oct 17, 2024 12:5100000Error: expected "1002155 ", got "1232859 "
54Oct 17, 2024 12:5100000Error: expected "624675 ", got "625029 "
55Oct 17, 2024 12:5100000Error: expected "2765421 ", got "2838542 "
56Oct 17, 2024 12:5100000Error: expected "1251073 ", got "1271029 "
57Oct 17, 2024 12:5300000Error: expected "1583962 ", got "1646565 "
58Oct 17, 2024 12:5300000Error: expected "770976 ", got "1005088 "
59Oct 17, 2024 12:5300000Error: expected "1242582 ", got "1254971 "
60Oct 17, 2024 12:5300000Error: expected "2764629 ", got "6546053 "
61Oct 17, 2024 12:5300000Error: No answer to stdout. Died or killed?
62Oct 17, 2024 12:5300000Error: No answer to stdout. Died or killed?
63Oct 17, 2024 12:5300000Error: expected "1799286 ", got "1842217 "
64Oct 17, 2024 12:5300000Error: No answer to stdout. Died or killed?
65Oct 17, 2024 12:5300000Error: expected "1294343 ", got "1294491 "
66Oct 17, 2024 12:5300000Error: expected "1762377 ", got "2068710 "
67Oct 17, 2024 12:5300000Error: expected "1705893 ", got "1706061 "
68Oct 17, 2024 12:5300000Error: No answer to stdout. Died or killed?
69Oct 17, 2024 12:5300000Error: expected "884881 ", got "886365 "
70Oct 17, 2024 12:5300000Error: expected "760702 ", got "910336 "
71Oct 17, 2024 12:5300000Error: No answer to stdout. Died or killed?
72Oct 17, 2024 12:5300000Error: expected "1859270 ", got "1861831 "
73Oct 17, 2024 12:5300000Error: expected "1839496 ", got "2199617 "
74Oct 17, 2024 12:5400000Error: expected "1583840 ", got "1710263 "
75Oct 17, 2024 12:5400000Error: expected "1240608 ", got "1240499 "
76Oct 17, 2024 12:5400000Error: No answer to stdout. Died or killed?
77Oct 17, 2024 12:5400000Error: expected "1130970 ", got "1199913 "
78Oct 17, 2024 12:5400000Error: No answer to stdout. Died or killed?
79Oct 17, 2024 12:5400000Error: No answer to stdout. Died or killed?
80Oct 17, 2024 12:5400000Error: expected "1560155 ", got "1677209 "
81Oct 17, 2024 12:5400000Error: expected "1289551 ", got "1336049 "
82Oct 17, 2024 12:5400000Error: No answer to stdout. Died or killed?
83Oct 17, 2024 12:5500000Error: expected "878188 ", got "878568 "
84Oct 17, 2024 12:5500000Error: expected "1805682 ", got "1807765 "
85Oct 17, 2024 12:5500000Error: expected "1585408 ", got "1788309 "
86Oct 17, 2024 12:5500000Error: No answer to stdout. Died or killed?
87Oct 17, 2024 12:5500000Error: No answer to stdout. Died or killed?
88Oct 17, 2024 12:5500000Error: No answer to stdout. Died or killed?
89Oct 17, 2024 12:5500000Error: expected "2010264 ", got "2018362 "
90Oct 17, 2024 12:5500000Error: No answer to stdout. Died or killed?
91Oct 17, 2024 12:5500000Error: No answer to stdout. Died or killed?
92Oct 17, 2024 12:5600000Error: expected "885047 ", got "885185 "
93Oct 17, 2024 12:5600000Error: No answer to stdout. Died or killed?
94Oct 17, 2024 12:5600000Error: expected "896141 ", got "898968 "
95Oct 17, 2024 12:5600000Error: No answer to stdout. Died or killed?
96Oct 17, 2024 12:5600000Error: expected "2594262 ", got "2626551 "
97Oct 17, 2024 12:5600000Error: expected "1299498 ", got "1337616 "
98Oct 17, 2024 12:5600000Error: expected "2755908 ", got "2864333 "
99Oct 17, 2024 12:5600000Error: expected "1613369 ", got "1819560 "
100Oct 17, 2024 12:5600000Error: expected "1300720 ", got "1369685 "
101Oct 17, 2024 12:5600000Error: No answer to stdout. Died or killed?
102Oct 17, 2024 12:5600000Error: No answer to stdout. Died or killed?
103Oct 17, 2024 12:5600000Error: No answer to stdout. Died or killed?
104Oct 17, 2024 12:5600000Error: expected "2020862 ", got "2023614 "
105Oct 17, 2024 12:5600000Error: expected "1259691 ", got "1303651 "
106Oct 17, 2024 12:5600000Error: No answer to stdout. Died or killed?
107Oct 17, 2024 12:5600000Error: No answer to stdout. Died or killed?
108Oct 17, 2024 12:5600000Error: expected "846344 ", got "1056812 "
109Oct 17, 2024 12:5800000Error: No answer to stdout. Died or killed?
110Oct 17, 2024 12:5800000Error: expected "1214933 ", got "1215095 "
111Oct 17, 2024 12:5800000Error: expected "1945092 ", got "2047684 "
112Oct 17, 2024 12:5800000Error: No answer to stdout. Died or killed?
113Oct 17, 2024 12:5800000Error: expected "3464189 ", got "3465098 "
114Oct 17, 2024 12:5800000Error: No answer to stdout. Died or killed?
115Oct 17, 2024 12:5800000Error: expected "1179562 ", got "1482616 "
116Oct 17, 2024 12:5800000Error: expected "702524 ", got "810453 "
117Oct 17, 2024 12:5800000Error: No answer to stdout. Died or killed?
118Oct 17, 2024 12:5800000Error: No answer to stdout. Died or killed?
119Oct 17, 2024 12:5800000Error: expected "1925549 ", got "2150002 "
120Oct 17, 2024 12:5800000Error: expected "3363940 ", got "3533150 "
121Oct 17, 2024 12:5800000Error: expected "2835448 ", got "3764196 "
122Oct 17, 2024 12:5800000Error: expected "2965314 ", got "2981771 "
123Oct 17, 2024 12:5800000Error: expected "2264136 ", got "2320409 "
124Oct 17, 2024 12:5800000Error: expected "2884539 ", got "2884879 "
125Oct 17, 2024 12:5800000Error: expected "2376382 ", got "2376846 "
126Oct 17, 2024 12:5800000Error: expected "1182406 ", got "1241695 "
127Oct 17, 2024 12:5800000Error: expected "2150928 ", got "2186635 "
128Oct 17, 2024 12:5800000Error: expected "2448670 ", got "2537024 "
129Oct 17, 2024 12:5800000Error: expected "2707430 ", got "2819951 "
130Oct 17, 2024 12:5800000Error: expected "1511727 ", got "1527126 "
131Oct 17, 2024 13:0000000Error: expected "2114961 ", got "2118709 "
132Oct 17, 2024 13:0000000Error: expected "2083154 ", got "2083121 "
133Oct 17, 2024 13:0000000Error: No answer to stdout. Died or killed?
134Oct 17, 2024 13:0000000Error: expected "1383600 ", got "1384278 "
135Oct 17, 2024 13:0000000Error: expected "755597 ", got "809123 "
136Oct 17, 2024 13:0000000Error: No answer to stdout. Died or killed?
137Oct 17, 2024 13:0100000Error: expected "1340311 ", got "1364700 "
138Oct 17, 2024 13:0100000Error: expected "1749790 ", got "1761118 "
139Oct 17, 2024 13:0100000Error: expected "1418024 ", got "1515508 "
140Oct 17, 2024 13:0100000Error: No answer to stdout. Died or killed?
141Oct 17, 2024 13:0100000Error: No answer to stdout. Died or killed?
142Oct 17, 2024 13:0100000Error: expected "2271489 ", got "2270318 "
143Oct 17, 2024 13:0100000Error: No answer to stdout. Died or killed?
144Oct 17, 2024 13:0100000Error: expected "979475 ", got "1000776 "
145Oct 17, 2024 13:0100000Error: expected "2183679 ", got "2183921 "
146Oct 17, 2024 13:0100000Error: No answer to stdout. Died or killed?