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 14:25@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 16, 2024 14:2500000Error: No answer to stdout. Died or killed?
2Oct 16, 2024 14:2500000Error: No answer to stdout. Died or killed?
3Oct 16, 2024 14:2500000Error: expected "1232077 ", got "1264302 "
4Oct 16, 2024 14:2500000Error: expected "1249751 ", got "1519319 "
5Oct 16, 2024 14:2500000Error: expected "801674 ", got "813775 "
6Oct 16, 2024 14:2500000Error: expected "1395967 ", got "1469601 "
7Oct 16, 2024 14:2500000Error: expected "2207259 ", got "2211285 "
8Oct 16, 2024 14:2500000Error: No answer to stdout. Died or killed?
9Oct 16, 2024 14:2500000Error: No answer to stdout. Died or killed?
10Oct 16, 2024 14:2500000Error: No answer to stdout. Died or killed?
11Oct 16, 2024 14:2500000Error: No answer to stdout. Died or killed?
12Oct 16, 2024 14:2500000Error: expected "1219976 ", got "1432845 "
13Oct 16, 2024 14:2500000Error: expected "1499869 ", got "1501195 "
14Oct 16, 2024 14:2500000Error: No answer to stdout. Died or killed?
15Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
16Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
17Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
18Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
19Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
20Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
21Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
22Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
23Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
24Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
25Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
26Oct 16, 2024 14:2600000Error: expected "1087613 ", got "1122948 "
27Oct 16, 2024 14:2600000Error: expected "1089463 ", got "1269677 "
28Oct 16, 2024 14:2600000Error: expected "1059281 ", got "1103492 "
29Oct 16, 2024 14:2600000Error: expected "2689312 ", got "2697763 "
30Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
31Oct 16, 2024 14:2600000Error: expected "2552838 ", got "2632788 "
32Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
33Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
34Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
35Oct 16, 2024 14:2600000Error: expected "779874 ", got "818591 "
36Oct 16, 2024 14:2600000Error: No answer to stdout. Died or killed?
37Oct 16, 2024 14:2600000Error: expected "1355278 ", got "1446539 "
38Oct 16, 2024 14:2600000Error: expected "2079556 ", got "2158477 "
39Oct 16, 2024 14:2800000Error: expected "1171919 ", got "1414915 "
40Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
41Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
42Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
43Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
44Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
45Oct 16, 2024 14:2800000Error: expected "1445769 ", got "1487406 "
46Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
47Oct 16, 2024 14:2800000Error: expected "1245251 ", got "1274508 "
48Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
49Oct 16, 2024 14:2800000Error: expected "1196405 ", got "1197526 "
50Oct 16, 2024 14:2800000Error: expected "1049974 ", got "1059672 "
51Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
52Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
53Oct 16, 2024 14:2800000Error: expected "2519381 ", got "2571883 "
54Oct 16, 2024 14:2800000Error: expected "1534192 ", got "1690148 "
55Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
56Oct 16, 2024 14:2800000Error: expected "1147572 ", got "1510074 "
57Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
58Oct 16, 2024 14:2800000Error: No answer to stdout. Died or killed?
59Oct 16, 2024 14:2800000Error: expected "591525 ", got "599027 "
60Oct 16, 2024 14:2800000Error: expected "1363258 ", got "1580038 "
61Oct 16, 2024 14:2800000Error: expected "1755989 ", got "1865681 "
62Oct 16, 2024 14:2900000Error: No answer to stdout. Died or killed?
63Oct 16, 2024 14:2900000Error: expected "711606 ", got "713400 "
64Oct 16, 2024 14:2900000Error: No answer to stdout. Died or killed?
65Oct 16, 2024 14:2900000Error: No answer to stdout. Died or killed?
66Oct 16, 2024 14:2900000Error: No answer to stdout. Died or killed?
67Oct 16, 2024 14:2900000Error: No answer to stdout. Died or killed?
68Oct 16, 2024 14:2900000Error: No answer to stdout. Died or killed?
69Oct 16, 2024 14:3000000Error: No answer to stdout. Died or killed?
70Oct 16, 2024 14:3000000Error: expected "1861091 ", got "1860806 "
71Oct 16, 2024 14:3000000Error: No answer to stdout. Died or killed?
72Oct 16, 2024 14:3000000Error: expected "1454430 ", got "1452280 "
73Oct 16, 2024 14:3000000Error: expected "1512216 ", got "7204983 "
74Oct 16, 2024 14:3000000Error: No answer to stdout. Died or killed?
75Oct 16, 2024 14:3000000Error: No answer to stdout. Died or killed?
76Oct 16, 2024 14:3000000Error: No answer to stdout. Died or killed?
77Oct 16, 2024 14:3000000Error: expected "2389696 ", got "2556342 "
78Oct 16, 2024 14:3000000Error: No answer to stdout. Died or killed?
79Oct 16, 2024 14:3000000Error: No answer to stdout. Died or killed?
80Oct 16, 2024 14:3000000Error: expected "2582460 ", got "2596945 "
81Oct 16, 2024 14:3100000Error: expected "837039 ", got "1087240 "
82Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
83Oct 16, 2024 14:3100000Error: expected "2182245 ", got "3486172 "
84Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
85Oct 16, 2024 14:3100000Error: expected "1152802 ", got "1161815 "
86Oct 16, 2024 14:3100000Error: expected "2341722 ", got "2341874 "
87Oct 16, 2024 14:3100000Error: expected "1434761 ", got "1649512 "
88Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
89Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
90Oct 16, 2024 14:3100000Error: expected "1866914 ", got "1894278 "
91Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
92Oct 16, 2024 14:3100000Error: expected "1475855 ", got "1498336 "
93Oct 16, 2024 14:3100000Error: expected "2492601 ", got "2503618 "
94Oct 16, 2024 14:3100000Error: expected "844263 ", got "844511 "
95Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
96Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
97Oct 16, 2024 14:3100000Error: expected "1776834 ", got "2091861 "
98Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
99Oct 16, 2024 14:3100000Error: expected "2025740 ", got "2256369 "
100Oct 16, 2024 14:3100000Error: expected "933929 ", got "936967 "
101Oct 16, 2024 14:3100000Error: expected "773719 ", got "952572 "
102Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
103Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
104Oct 16, 2024 14:3100000Error: No answer to stdout. Died or killed?
105Oct 16, 2024 14:3100000Error: expected "2438198 ", got "2704834 "
106Oct 16, 2024 14:3100000Error: expected "746528 ", got "783359 "
107Oct 16, 2024 14:3200000Error: expected "1620843 ", got "1626299 "
108Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
109Oct 16, 2024 14:3200000Error: expected "855669 ", got "871062 "
110Oct 16, 2024 14:3200000Error: expected "1393259 ", got "1398775 "
111Oct 16, 2024 14:3200000Error: expected "2080920 ", got "2280413 "
112Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
113Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
114Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
115Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
116Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
117Oct 16, 2024 14:3200000Error: expected "748371 ", got "786905 "
118Oct 16, 2024 14:3200000Error: expected "939614 ", got "1073744 "
119Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
120Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
121Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
122Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
123Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
124Oct 16, 2024 14:3200000Error: expected "1900791 ", got "1914606 "
125Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
126Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?
127Oct 16, 2024 14:3200000Error: expected "631418 ", got "644567 "
128Oct 16, 2024 14:3200000Error: No answer to stdout. Died or killed?