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 listFeb 15, 2024 18:05__Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 15, 2024 18:0500000Error: No answer to stdout. Died or killed?
2Feb 15, 2024 18:0500000Error: No answer to stdout. Died or killed?
3Feb 15, 2024 18:0500000Error: expected "667719 ", got "667638"
4Feb 15, 2024 18:0500000Error: expected "1170177 ", got "1171068"
5Feb 15, 2024 18:0600000Error: expected "754255 ", got "753387"
6Feb 15, 2024 18:0600000Error: expected "635964 ", got "635921"
7Feb 15, 2024 18:0600000Error: expected "1769632 ", got "1761450"
8Feb 15, 2024 18:0600000Error: expected "1310577 ", got "1334786"
9Feb 15, 2024 18:0600000Error: No answer to stdout. Died or killed?
10Feb 15, 2024 18:0600000Error: No answer to stdout. Died or killed?
11Feb 15, 2024 18:0600000Error: expected "2177866 ", got "2179504"
12Feb 15, 2024 18:0800000Error: No answer to stdout. Died or killed?
13Feb 15, 2024 18:0800000Error: No answer to stdout. Died or killed?
14Feb 15, 2024 18:0800000Error: expected "1021412 ", got "1031793"
15Feb 15, 2024 18:0800000Error: expected "1383023 ", got "1377430"
16Feb 15, 2024 18:0800000Error: expected "1893320 ", got "1893173"
17Feb 15, 2024 18:0800000Error: expected "647001 ", got "646338"
18Feb 15, 2024 18:0800000Error: expected "1609486 ", got "1608066"
19Feb 15, 2024 18:0900000Error: expected "1412208 ", got "1408992"
20Feb 15, 2024 18:0900000Error: No answer to stdout. Died or killed?
21Feb 15, 2024 18:0900000Error: No answer to stdout. Died or killed?
22Feb 15, 2024 18:0900000Error: expected "1349784 ", got "1407187"
23Feb 15, 2024 18:0900000Error: No answer to stdout. Died or killed?
24Feb 15, 2024 18:0900000Error: expected "1923603 ", got "1912728"
25Feb 15, 2024 18:0900000Error: No answer to stdout. Died or killed?
26Feb 15, 2024 18:0900000Error: expected "1170607 ", got "1170569"
27Feb 15, 2024 18:1000000Error: expected "1340254 ", got "1354087"
28Feb 15, 2024 18:1000000Error: No answer to stdout. Died or killed?
29Feb 15, 2024 18:1000000Error: expected "1368632 ", got "1368113"
30Feb 15, 2024 18:1000000Error: No answer to stdout. Died or killed?
31Feb 15, 2024 18:1000000Error: expected "661475 ", got "653472"
32Feb 15, 2024 18:1000000Error: No answer to stdout. Died or killed?
33Feb 15, 2024 18:1000000Error: expected "2620913 ", got "2601308"
34Feb 15, 2024 18:1000000Error: No answer to stdout. Died or killed?
35Feb 15, 2024 18:1000000Error: expected "831076 ", got "827830"
36Feb 15, 2024 18:1000000Error: expected "727186 ", got "765852"
37Feb 15, 2024 18:1000000Error: expected "1590783 ", got "1484370"
38Feb 15, 2024 18:1100000Error: No answer to stdout. Died or killed?
39Feb 15, 2024 18:1100000Error: expected "2156707 ", got "2162326"
40Feb 15, 2024 18:1100000Error: No answer to stdout. Died or killed?
41Feb 15, 2024 18:1100000Error: expected "2555540 ", got "2554716"
42Feb 15, 2024 18:1100000Error: expected "832191 ", got "830395"
43Feb 15, 2024 18:1100000Error: No answer to stdout. Died or killed?
44Feb 15, 2024 18:1100000Error: expected "2447869 ", got "2459087"
45Feb 15, 2024 18:1100000Error: expected "2742022 ", got "2739550"
46Feb 15, 2024 18:1100000Error: No answer to stdout. Died or killed?
47Feb 15, 2024 18:1100000Error: expected "1276888 ", got "1274804"
48Feb 15, 2024 18:1100000Error: No answer to stdout. Died or killed?
49Feb 15, 2024 18:1100000Error: expected "2098291 ", got "2097527"
50Feb 15, 2024 18:1200000Error: expected "746366 ", got "744489"
51Feb 15, 2024 18:1200000Error: No answer to stdout. Died or killed?
52Feb 15, 2024 18:1300000Error: expected "597711 ", got "594780"
53Feb 15, 2024 18:1300000Error: No answer to stdout. Died or killed?
54Feb 15, 2024 18:1300000Error: No answer to stdout. Died or killed?
55Feb 15, 2024 18:1300000Error: expected "1283561 ", got "1293209"
56Feb 15, 2024 18:1300000Error: expected "1200007 ", got "1196357"
57Feb 15, 2024 18:1300000Error: expected "572907 ", got "570016"
58Feb 15, 2024 18:1400000Error: expected "2055771 ", got "2053724"
59Feb 15, 2024 18:1400000Error: expected "1433924 ", got "1423745"
60Feb 15, 2024 18:1400000Error: expected "1098063 ", got "1084331"
61Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
62Feb 15, 2024 18:1400000Error: expected "913496 ", got "1071773"
63Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
64Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
65Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
66Feb 15, 2024 18:1400000Error: expected "1145554 ", got "1135435"
67Feb 15, 2024 18:1400000Error: expected "774317 ", got "761009"
68Feb 15, 2024 18:1400000Error: expected "2436043 ", got "2431152"
69Feb 15, 2024 18:1400000Error: expected "569286 ", got "565837"
70Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
71Feb 15, 2024 18:1400000Error: expected "2346310 ", got "2334858"
72Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
73Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
74Feb 15, 2024 18:1400000Error: expected "2358586 ", got "2381684"
75Feb 15, 2024 18:1400000Error: expected "3072946 ", got "3070885"
76Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
77Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
78Feb 15, 2024 18:1400000Error: No answer to stdout. Died or killed?
79Feb 15, 2024 18:1400000Error: expected "1156628 ", got "1158022"
80Feb 15, 2024 18:1500000Error: expected "1196308 ", got "1226736"
81Feb 15, 2024 18:1500000Error: expected "1987888 ", got "1988947"
82Feb 15, 2024 18:1500000Error: expected "870370 ", got "870328"
83Feb 15, 2024 18:1500000Error: expected "997532 ", got "1101266"
84Feb 15, 2024 18:1500000Error: expected "873158 ", got "877404"
85Feb 15, 2024 18:1500000Error: expected "2127995 ", got "2180674"
86Feb 15, 2024 18:1500000Error: No answer to stdout. Died or killed?
87Feb 15, 2024 18:1500000Error: expected "1255095 ", got "1253199"
88Feb 15, 2024 18:1500000Error: expected "2003702 ", got "2034515"
89Feb 15, 2024 18:1500000Error: expected "1139317 ", got "1140302"
90Feb 15, 2024 18:1600000Error: No answer to stdout. Died or killed?
91Feb 15, 2024 18:1600000Error: expected "1450746 ", got "1451604"
92Feb 15, 2024 18:1600000Error: expected "3390243 ", got "3402869"
93Feb 15, 2024 18:1600000Error: expected "781264 ", got "744992"
94Feb 15, 2024 18:1600000Error: expected "1049243 ", got "1048355"
95Feb 15, 2024 18:1600000Error: expected "1397503 ", got "1396865"
96Feb 15, 2024 18:1600000Error: expected "1893211 ", got "1910156"
97Feb 15, 2024 18:1600000Error: expected "1755622 ", got "1756546"
98Feb 15, 2024 18:1600000Error: No answer to stdout. Died or killed?
99Feb 15, 2024 18:1600000Error: expected "2585838 ", got "2570298"
100Feb 15, 2024 18:1600000Error: expected "1812951 ", got "1834121"
101Feb 15, 2024 18:1600000Error: expected "1867775 ", got "1866668"
102Feb 15, 2024 18:1600000Error: expected "2044751 ", got "2087905"
103Feb 15, 2024 18:1600000Error: expected "1567880 ", got "1557418"
104Feb 15, 2024 18:1600000Error: expected "1516909 ", got "1569959"
105Feb 15, 2024 18:1700000Error: expected "2324177 ", got "2323201"
106Feb 15, 2024 18:1700000Error: expected "1287436 ", got "1350918"
107Feb 15, 2024 18:1700000Error: expected "1184365 ", got "1460928"
108Feb 15, 2024 18:1700000Error: expected "1001364 ", got "998750"
109Feb 15, 2024 18:1700000Error: No answer to stdout. Died or killed?
110Feb 15, 2024 18:1700000Error: expected "1119674 ", got "1398100"
111Feb 15, 2024 18:1700000Error: expected "1386033 ", got "1376295"
112Feb 15, 2024 18:1700000Error: expected "2888517 ", got "2885416"
113Feb 15, 2024 18:1700000Error: expected "1209510 ", got "1209909"
114Feb 15, 2024 18:1700000Error: No answer to stdout. Died or killed?
115Feb 15, 2024 18:1700000Error: expected "924908 ", got "935849"
116Feb 15, 2024 18:1700000Error: expected "3277358 ", got "3283645"
117Feb 15, 2024 18:1800000Error: expected "1132116 ", got "1064231"
118Feb 15, 2024 18:1800000Error: No answer to stdout. Died or killed?
119Feb 15, 2024 18:1800000Error: expected "917655 ", got "914557"
120Feb 15, 2024 18:1800000Error: No answer to stdout. Died or killed?
121Feb 15, 2024 18:1800000Error: expected "1208483 ", got "1207684"
122Feb 15, 2024 18:1800000Error: No answer to stdout. Died or killed?
123Feb 15, 2024 18:1800000Error: expected "1906009 ", got "1895579"
124Feb 15, 2024 18:1800000Error: expected "949724 ", got "949034"
125Feb 15, 2024 18:1800000Error: expected "790332 ", got "790266"
126Feb 15, 2024 18:1800000Error: expected "668415 ", got "672486"
127Feb 15, 2024 18:1800000Error: No answer to stdout. Died or killed?
128Feb 15, 2024 18:1800000Error: expected "937564 ", got "949906"
129Feb 15, 2024 18:1800000Error: No answer to stdout. Died or killed?
130Feb 15, 2024 18:1800000Error: expected "1614310 ", got "1613210"
131Feb 15, 2024 18:1800000Error: No answer to stdout. Died or killed?
132Feb 15, 2024 18:1900000Error: No answer to stdout. Died or killed?
133Feb 15, 2024 18:1900000Error: No answer to stdout. Died or killed?
134Feb 15, 2024 18:1900000Error: expected "1534747 ", got "1531886"
135Feb 15, 2024 18:1900000Error: expected "2703338 ", got "2703127"
136Feb 15, 2024 18:1900000Error: expected "808786 ", got "807756"
137Feb 15, 2024 18:1900000Error: No answer to stdout. Died or killed?
138Feb 15, 2024 18:1900000Error: expected "1094750 ", got "1055882"
139Feb 15, 2024 18:1900000Error: expected "2367565 ", got "2365642"
140Feb 15, 2024 18:1900000Error: expected "1259803 ", got "1259600"
141Feb 15, 2024 18:1900000Error: expected "712153 ", got "743720"
142Feb 15, 2024 18:1900000Error: expected "1447308 ", got "1529717"
143Feb 15, 2024 18:1900000Error: expected "2431617 ", got "2454062"
144Feb 15, 2024 18:1900000Error: expected "1030012 ", got "1094533"
145Feb 15, 2024 18:1900000Error: No answer to stdout. Died or killed?
146Feb 15, 2024 18:1900000Error: expected "856624 ", got "856400"
147Feb 15, 2024 18:2000000Error: expected "1275550 ", got "1276539"
148Feb 15, 2024 18:2000000Error: expected "831227 ", got "827780"
149Feb 15, 2024 18:2100000Error: expected "1404349 ", got "1403964"
150Feb 15, 2024 18:2100000Error: expected "1050315 ", got "1049415"
151Feb 15, 2024 18:0943,56988,280,31225,269,81102,199,552
152Feb 15, 2024 18:1243,64795,356,94225,315,45402,195,456
153Feb 15, 2024 18:0643,67384,045,94525,330,54602,187,264
154Feb 15, 2024 18:0943,68695,892,95225,337,85802,183,168
155Feb 15, 2024 18:2044,001108,719,28625,520,58702,199,552
156Feb 15, 2024 18:0644,09191,021,35225,572,79902,195,456
157Feb 15, 2024 18:2044,20391,238,64925,637,59202,187,264
158Feb 15, 2024 18:0944,23795,347,20025,657,57602,199,552
159Feb 15, 2024 18:1244,49495,326,57025,806,72502,191,360
160Feb 15, 2024 18:2044,50882,201,24125,814,82302,199,552
161Feb 15, 2024 18:0644,94098,720,74826,064,99402,310,144
162Feb 15, 2024 18:1245,46793,774,63226,370,80402,191,360