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 17:40__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 17:4300000Error: No answer to stdout. Died or killed?
2Feb 15, 2024 17:4400000Error: No answer to stdout. Died or killed?
3Feb 15, 2024 17:4400000Error: expected "1984782 ", got "1983180"
4Feb 15, 2024 17:4400000Error: expected "2855383 ", got "2852197"
5Feb 15, 2024 17:4400000Error: expected "2254411 ", got "2243676"
6Feb 15, 2024 17:4400000Error: expected "2234025 ", got "2258488"
7Feb 15, 2024 17:4400000Error: No answer to stdout. Died or killed?
8Feb 15, 2024 17:4300000Error: expected "1806051 ", got "1804036"
9Feb 15, 2024 17:4300000Error: No answer to stdout. Died or killed?
10Feb 15, 2024 17:4400000Error: expected "1931106 ", got "2138053"
11Feb 15, 2024 17:4300000Error: expected "1671391 ", got "1776840"
12Feb 15, 2024 17:4300000Error: expected "1134351 ", got "1160151"
13Feb 15, 2024 17:4300000Error: expected "707508 ", got "704727"
14Feb 15, 2024 17:4300000Error: expected "1637805 ", got "1636071"
15Feb 15, 2024 17:4300000Error: expected "1144544 ", got "1144242"
16Feb 15, 2024 17:4300000Error: expected "1095048 ", got "1093202"
17Feb 15, 2024 17:4300000Error: No answer to stdout. Died or killed?
18Feb 15, 2024 17:4300000Error: expected "1172922 ", got "1174945"
19Feb 15, 2024 17:5800000Error: expected "816103 ", got "815429"
20Feb 15, 2024 17:5800000Error: No answer to stdout. Died or killed?
21Feb 15, 2024 17:5800000Error: No answer to stdout. Died or killed?
22Feb 15, 2024 17:5800000Error: expected "522091 ", got "517948"
23Feb 15, 2024 17:5800000Error: No answer to stdout. Died or killed?
24Feb 15, 2024 17:5800000Error: expected "1721722 ", got "1716828"
25Feb 15, 2024 17:5800000Error: No answer to stdout. Died or killed?
26Feb 15, 2024 17:5800000Error: expected "1886566 ", got "1882130"
27Feb 15, 2024 17:5800000Error: No answer to stdout. Died or killed?
28Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
29Feb 15, 2024 17:5700000Error: expected "2733114 ", got "2728742"
30Feb 15, 2024 17:5700000Error: expected "1852369 ", got "1842009"
31Feb 15, 2024 17:5700000Error: No answer to stdout. Died or killed?
32Feb 15, 2024 17:5700000Error: No answer to stdout. Died or killed?
33Feb 15, 2024 17:5700000Error: No answer to stdout. Died or killed?
34Feb 15, 2024 17:5700000Error: expected "1141122 ", got "1137168"
35Feb 15, 2024 17:4400000Error: expected "848844 ", got "851986"
36Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
37Feb 15, 2024 17:4000000Error: No answer to stdout. Died or killed?
38Feb 15, 2024 17:4000000Error: expected "2272259 ", got "2270848"
39Feb 15, 2024 17:4000000Error: No answer to stdout. Died or killed?
40Feb 15, 2024 17:4000000Error: expected "1035201 ", got "1028428"
41Feb 15, 2024 17:4000000Error: No answer to stdout. Died or killed?
42Feb 15, 2024 17:4000000Error: No answer to stdout. Died or killed?
43Feb 15, 2024 17:4100000Error: No answer to stdout. Died or killed?
44Feb 15, 2024 17:4100000Error: expected "2395961 ", got "2391003"
45Feb 15, 2024 17:4100000Error: expected "1028721 ", got "1048559"
46Feb 15, 2024 17:4100000Error: expected "722403 ", got "720422"
47Feb 15, 2024 17:4100000Error: No answer to stdout. Died or killed?
48Feb 15, 2024 17:4100000Error: expected "876550 ", got "871841"
49Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
50Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
51Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
52Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
53Feb 15, 2024 17:4200000Error: expected "986463 ", got "1034184"
54Feb 15, 2024 17:4200000Error: expected "750602 ", got "748751"
55Feb 15, 2024 17:4200000Error: expected "2539042 ", got "2556775"
56Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
57Feb 15, 2024 17:4200000Error: expected "827871 ", got "827484"
58Feb 15, 2024 17:4200000Error: expected "1176247 ", got "1172536"
59Feb 15, 2024 17:4200000Error: No answer to stdout. Died or killed?
60Feb 15, 2024 17:4200000Error: expected "2530257 ", got "2529393"
61Feb 15, 2024 17:4200000Error: expected "1833702 ", got "1833526"
62Feb 15, 2024 17:4044,56188,836,34725,845,13602,199,552
63Feb 15, 2024 17:5844,731113,434,60525,944,25902,191,360
64Feb 15, 2024 17:5845,05594,435,85826,131,78302,199,552
65Feb 15, 2024 17:4445,07884,445,70726,144,99902,187,264
66Feb 15, 2024 17:4445,09392,294,28526,154,14102,191,360
67Feb 15, 2024 17:5845,35896,675,59626,307,52302,187,264
68Feb 15, 2024 17:4445,39891,509,94026,330,74302,195,456
69Feb 15, 2024 17:4045,74293,566,75426,530,31002,195,456
70Feb 15, 2024 17:4045,811105,601,87426,570,12602,187,264