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 19:02__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 19:0500000Error: expected "1607553 ", got "1582933"
2Feb 15, 2024 19:0600000Error: expected "931954 ", got "931926"
3Feb 15, 2024 19:0600000Error: No answer to stdout. Died or killed?
4Feb 15, 2024 19:0600000Error: expected "801957 ", got "801698"
5Feb 15, 2024 19:0600000Error: expected "1994633 ", got "1992372"
6Feb 15, 2024 19:0600000Error: expected "941004 ", got "1008664"
7Feb 15, 2024 19:0600000Error: No answer to stdout. Died or killed?
8Feb 15, 2024 19:0600000Error: No answer to stdout. Died or killed?
9Feb 15, 2024 19:0600000Error: expected "659833 ", got "648278"
10Feb 15, 2024 19:0500000Error: No answer to stdout. Died or killed?
11Feb 15, 2024 19:0700000Error: expected "1037037 ", got "1035993"
12Feb 15, 2024 19:0500000Error: expected "1226946 ", got "1214717"
13Feb 15, 2024 19:0500000Error: expected "1509615 ", got "1506947"
14Feb 15, 2024 19:0500000Error: No answer to stdout. Died or killed?
15Feb 15, 2024 19:0500000Error: expected "2540831 ", got "2582756"
16Feb 15, 2024 19:0500000Error: No answer to stdout. Died or killed?
17Feb 15, 2024 19:0500000Error: expected "1203388 ", got "1689250"
18Feb 15, 2024 19:0500000Error: expected "1091408 ", got "1084160"
19Feb 15, 2024 19:0500000Error: No answer to stdout. Died or killed?
20Feb 15, 2024 19:0500000Error: No answer to stdout. Died or killed?
21Feb 15, 2024 19:0700000Error: expected "1536282 ", got "1544828"
22Feb 15, 2024 19:0800000Error: No answer to stdout. Died or killed?
23Feb 15, 2024 19:0800000Error: No answer to stdout. Died or killed?
24Feb 15, 2024 19:0800000Error: No answer to stdout. Died or killed?
25Feb 15, 2024 19:0700000Error: expected "1342517 ", got "1530762"
26Feb 15, 2024 19:0700000Error: expected "722477 ", got "709336"
27Feb 15, 2024 19:0700000Error: No answer to stdout. Died or killed?
28Feb 15, 2024 19:0700000Error: No answer to stdout. Died or killed?
29Feb 15, 2024 19:0700000Error: expected "1645603 ", got "1651526"
30Feb 15, 2024 19:0700000Error: expected "655616 ", got "649260"
31Feb 15, 2024 19:0500000Error: expected "825297 ", got "819597"
32Feb 15, 2024 19:0700000Error: expected "1282981 ", got "1271802"
33Feb 15, 2024 19:0700000Error: expected "873756 ", got "872646"
34Feb 15, 2024 19:0700000Error: No answer to stdout. Died or killed?
35Feb 15, 2024 19:0700000Error: expected "1438048 ", got "1457094"
36Feb 15, 2024 19:0700000Error: expected "697646 ", got "689597"
37Feb 15, 2024 19:0700000Error: expected "2238506 ", got "2234678"
38Feb 15, 2024 19:0700000Error: No answer to stdout. Died or killed?
39Feb 15, 2024 19:0700000Error: expected "1825207 ", got "1824282"
40Feb 15, 2024 19:0300000Error: expected "893866 ", got "893210"
41Feb 15, 2024 19:0400000Error: No answer to stdout. Died or killed?
42Feb 15, 2024 19:0300000Error: No answer to stdout. Died or killed?
43Feb 15, 2024 19:0300000Error: expected "1376037 ", got "1378331"
44Feb 15, 2024 19:0300000Error: No answer to stdout. Died or killed?
45Feb 15, 2024 19:0300000Error: expected "1002405 ", got "1043442"
46Feb 15, 2024 19:0300000Error: expected "1496519 ", got "1494619"
47Feb 15, 2024 19:0300000Error: expected "1886487 ", got "1899334"
48Feb 15, 2024 19:0300000Error: expected "869156 ", got "888480"
49Feb 15, 2024 19:0300000Error: expected "1607660 ", got "1606890"
50Feb 15, 2024 19:0400000Error: No answer to stdout. Died or killed?
51Feb 15, 2024 19:0300000Error: No answer to stdout. Died or killed?
52Feb 15, 2024 19:0300000Error: No answer to stdout. Died or killed?
53Feb 15, 2024 19:0300000Error: expected "1364399 ", got "1393703"
54Feb 15, 2024 19:0300000Error: expected "1179066 ", got "1173385"
55Feb 15, 2024 19:0300000Error: expected "1733963 ", got "1728629"
56Feb 15, 2024 19:0200000Error: expected "698482 ", got "688634"
57Feb 15, 2024 19:0200000Error: No answer to stdout. Died or killed?
58Feb 15, 2024 19:0200000Error: No answer to stdout. Died or killed?
59Feb 15, 2024 19:0400000Error: expected "2472073 ", got "2475103"
60Feb 15, 2024 19:0500000Error: No answer to stdout. Died or killed?
61Feb 15, 2024 19:0500000Error: No answer to stdout. Died or killed?
62Feb 15, 2024 19:0500000Error: expected "637746 ", got "636347"
63Feb 15, 2024 19:0500000Error: expected "1667051 ", got "1749975"
64Feb 15, 2024 19:0400000Error: expected "1196971 ", got "1182284"
65Feb 15, 2024 19:0400000Error: expected "1350231 ", got "1349121"
66Feb 15, 2024 19:0400000Error: expected "1753383 ", got "1752952"
67Feb 15, 2024 19:0400000Error: expected "1196172 ", got "1229827"
68Feb 15, 2024 19:0400000Error: No answer to stdout. Died or killed?
69Feb 15, 2024 19:0200000Error: expected "1206396 ", got "1207076"
70Feb 15, 2024 19:0400000Error: expected "1021333 ", got "1017745"
71Feb 15, 2024 19:0400000Error: expected "655556 ", got "654332"
72Feb 15, 2024 19:0400000Error: expected "1166582 ", got "1162841"
73Feb 15, 2024 19:0400000Error: No answer to stdout. Died or killed?
74Feb 15, 2024 19:0400000Error: expected "753390 ", got "751068"
75Feb 15, 2024 19:0400000Error: No answer to stdout. Died or killed?
76Feb 15, 2024 19:0400000Error: No answer to stdout. Died or killed?
77Feb 15, 2024 19:0400000Error: No answer to stdout. Died or killed?