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 14, 2024 16:03__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 14, 2024 16:2900000Error: Killed by timeout
2Feb 14, 2024 16:2900000Error: Killed by timeout
3Feb 14, 2024 16:2900000Error: Killed by timeout
4Feb 14, 2024 16:2900000Error: expected "1868929 ", got "1875121"
5Feb 14, 2024 16:2900000Error: Killed by timeout
6Feb 14, 2024 16:2900000Error: Killed by timeout
7Feb 14, 2024 16:2900000Error: expected "1535583 ", got "1534293"
8Feb 14, 2024 16:2900000Error: Killed by timeout
9Feb 14, 2024 16:2900000Error: Killed by timeout
10Feb 14, 2024 16:2900000Error: expected "1435613 ", got "1436753"
11Feb 14, 2024 16:2900000Error: Killed by timeout
12Feb 14, 2024 16:2900000Error: Killed by timeout
13Feb 14, 2024 16:2900000Error: Killed by timeout
14Feb 14, 2024 16:2900000Error: Killed by timeout
15Feb 14, 2024 16:2900000Error: expected "1096221 ", got "1097197"
16Feb 14, 2024 16:2900000Error: Killed by timeout
17Feb 14, 2024 16:2900000Error: expected "2065077 ", got "2065033"
18Feb 14, 2024 16:2900000Error: Killed by timeout
19Feb 14, 2024 16:2900000Error: expected "1369473 ", got "1410129"
20Feb 14, 2024 16:2800000Error: Killed by timeout
21Feb 14, 2024 16:2800000Error: Killed by timeout
22Feb 14, 2024 16:2800000Error: expected "1063824 ", got "1138252"
23Feb 14, 2024 16:2800000Error: Killed by timeout
24Feb 14, 2024 16:2200000Error: Killed by timeout
25Feb 14, 2024 16:4000000Error: expected "1611911 ", got "1610541"
26Feb 14, 2024 16:4000000Error: Killed by timeout
27Feb 14, 2024 16:4000000Error: Killed by timeout
28Feb 14, 2024 16:4000000Error: Killed by timeout
29Feb 14, 2024 16:4000000Error: Killed by timeout
30Feb 14, 2024 16:4000000Error: Killed by timeout
31Feb 14, 2024 16:4000000Error: Killed by timeout
32Feb 14, 2024 16:4000000Error: Killed by timeout
33Feb 14, 2024 16:4000000Error: Killed by timeout
34Feb 14, 2024 16:4000000Error: Killed by timeout
35Feb 14, 2024 16:4000000Error: Killed by timeout
36Feb 14, 2024 16:4000000Error: Killed by timeout
37Feb 14, 2024 16:2200000Error: Killed by timeout
38Feb 14, 2024 16:4000000Error: expected "2259947 ", got "2259222"
39Feb 14, 2024 16:3900000Error: Killed by timeout
40Feb 14, 2024 16:3900000Error: Killed by timeout
41Feb 14, 2024 16:3900000Error: Killed by timeout
42Feb 14, 2024 16:3900000Error: Killed by timeout
43Feb 14, 2024 16:3900000Error: Killed by timeout
44Feb 14, 2024 16:3900000Error: expected "1922794 ", got "1922420"
45Feb 14, 2024 16:3900000Error: expected "1081301 ", got "1084306"
46Feb 14, 2024 16:3900000Error: expected "626755 ", got "625883"
47Feb 14, 2024 16:2900000Error: Killed by timeout
48Feb 14, 2024 16:2900000Error: expected "1336524 ", got "1351864"
49Feb 14, 2024 16:0700000Error: Killed by timeout
50Feb 14, 2024 16:1600000Error: expected "1014849 ", got "1014969"
51Feb 14, 2024 16:1100000Error: expected "1797943 ", got "1799262"
52Feb 14, 2024 16:1100000Error: Killed by timeout
53Feb 14, 2024 16:1000000Error: expected "2794478 ", got "2808095"
54Feb 14, 2024 16:1000000Error: Killed by timeout
55Feb 14, 2024 16:1000000Error: Killed by timeout
56Feb 14, 2024 16:1000000Error: Killed by timeout
57Feb 14, 2024 16:1000000Error: Killed by timeout
58Feb 14, 2024 16:1000000Error: Killed by timeout
59Feb 14, 2024 16:1000000Error: Killed by timeout
60Feb 14, 2024 16:1000000Error: Killed by timeout
61Feb 14, 2024 16:1600000Error: Killed by timeout
62Feb 14, 2024 16:0700000Error: Killed by timeout
63Feb 14, 2024 16:0700000Error: Killed by timeout
64Feb 14, 2024 16:0400000Error: Killed by timeout
65Feb 14, 2024 16:0400000Error: Killed by timeout
66Feb 14, 2024 16:0300000Error: Killed by timeout
67Feb 14, 2024 16:0300000Error: Killed by timeout
68Feb 14, 2024 16:0300000Error: Killed by timeout
69Feb 14, 2024 16:0300000Error: Killed by timeout
70Feb 14, 2024 16:0300000Error: expected "1376813 ", got "1385092"
71Feb 14, 2024 16:0300000Error: Killed by timeout
72Feb 14, 2024 16:0300000Error: expected "1983608 ", got "1983998"
73Feb 14, 2024 16:1600000Error: expected "1022186 ", got "1040429"
74Feb 14, 2024 16:2200000Error: expected "722326 ", got "721234"
75Feb 14, 2024 16:2200000Error: Killed by timeout
76Feb 14, 2024 16:2200000Error: Killed by timeout
77Feb 14, 2024 16:2200000Error: expected "3810168 ", got "3809588"
78Feb 14, 2024 16:2200000Error: Killed by timeout
79Feb 14, 2024 16:2200000Error: Killed by timeout
80Feb 14, 2024 16:2200000Error: Killed by timeout
81Feb 14, 2024 16:2200000Error: Killed by timeout
82Feb 14, 2024 16:2200000Error: Killed by timeout
83Feb 14, 2024 16:1700000Error: expected "1146010 ", got "1259711"
84Feb 14, 2024 16:1700000Error: expected "1480187 ", got "1487768"
85Feb 14, 2024 16:0300000Error: expected "2725653 ", got "2726931"
86Feb 14, 2024 16:1600000Error: Killed by timeout
87Feb 14, 2024 16:1600000Error: Killed by timeout
88Feb 14, 2024 16:1600000Error: expected "3095023 ", got "3095515"
89Feb 14, 2024 16:1600000Error: expected "726692 ", got "726713"
90Feb 14, 2024 16:1600000Error: Killed by timeout
91Feb 14, 2024 16:1600000Error: Killed by timeout
92Feb 14, 2024 16:1600000Error: Killed by timeout
93Feb 14, 2024 16:1600000Error: Killed by timeout
94Feb 14, 2024 16:1600000Error: Killed by timeout
95Feb 14, 2024 16:1600000Error: Killed by timeout