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 15, 2024 20:08@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 15, 2024 20:0800000Error: No answer to stdout. Died or killed?
2Oct 15, 2024 20:1000000Error: expected "1461510 ", got "1461622 "
3Oct 15, 2024 20:1000000Error: No answer to stdout. Died or killed?
4Oct 15, 2024 20:1000000Error: No answer to stdout. Died or killed?
5Oct 15, 2024 20:1000000Error: No answer to stdout. Died or killed?
6Oct 15, 2024 20:1000000Error: No answer to stdout. Died or killed?
7Oct 15, 2024 20:1000000Error: No answer to stdout. Died or killed?
8Oct 15, 2024 20:1000000Error: expected "1428626 ", got "1894856 "
9Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
10Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
11Oct 15, 2024 20:1100000Error: expected "838950 ", got "884312 "
12Oct 15, 2024 20:1100000Error: expected "967163 ", got "1063069 "
13Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
14Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
15Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
16Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
17Oct 15, 2024 20:1100000Error: expected "1336622 ", got "1338847 "
18Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
19Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
20Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
21Oct 15, 2024 20:1100000Error: expected "1552319 ", got "2213234 "
22Oct 15, 2024 20:1100000Error: No answer to stdout. Died or killed?
23Oct 15, 2024 20:1200000Error: expected "1648023 ", got "1713685 "
24Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
25Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
26Oct 15, 2024 20:1200000Error: expected "901669 ", got "904844 "
27Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
28Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
29Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
30Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
31Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
32Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
33Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
34Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
35Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
36Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
37Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
38Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
39Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
40Oct 15, 2024 20:1200000Error: No answer to stdout. Died or killed?
41Oct 15, 2024 20:1300000Error: No answer to stdout. Died or killed?
42Oct 15, 2024 20:1400000Error: No answer to stdout. Died or killed?
43Oct 15, 2024 20:1400000Error: No answer to stdout. Died or killed?
44Oct 15, 2024 20:1400000Error: No answer to stdout. Died or killed?
45Oct 15, 2024 20:1400000Error: expected "870541 ", got "895839 "
46Oct 15, 2024 20:1400000Error: No answer to stdout. Died or killed?
47Oct 15, 2024 20:1400000Error: No answer to stdout. Died or killed?
48Oct 15, 2024 20:1400000Error: expected "1474013 ", got "1474953 "
49Oct 15, 2024 20:1400000Error: No answer to stdout. Died or killed?
50Oct 15, 2024 20:1400000Error: No answer to stdout. Died or killed?
51Oct 15, 2024 20:1400000Error: expected "1789465 ", got "1854801 "
52Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
53Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
54Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
55Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
56Oct 15, 2024 20:1500000Error: expected "1987625 ", got "1988043 "
57Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
58Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
59Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
60Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
61Oct 15, 2024 20:1500000Error: No answer to stdout. Died or killed?
62Oct 15, 2024 20:1600000Error: No answer to stdout. Died or killed?
63Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
64Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
65Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
66Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
67Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
68Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
69Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
70Oct 15, 2024 20:1700000Error: No answer to stdout. Died or killed?
71Oct 15, 2024 20:2000000Error: expected "759754 ", got "763498 "
72Oct 15, 2024 20:2000000Error: expected "3659275 ", got "3687595 "
73Oct 15, 2024 20:2000000Error: No answer to stdout. Died or killed?
74Oct 15, 2024 20:2000000Error: No answer to stdout. Died or killed?
75Oct 15, 2024 20:2100000Error: No answer to stdout. Died or killed?
76Oct 15, 2024 20:2100000Error: expected "846076 ", got "899945 "
77Oct 15, 2024 20:2100000Error: No answer to stdout. Died or killed?
78Oct 15, 2024 20:2100000Error: No answer to stdout. Died or killed?
79Oct 15, 2024 20:2100000Error: No answer to stdout. Died or killed?
80Oct 15, 2024 20:2100000Error: No answer to stdout. Died or killed?
81Oct 15, 2024 20:2100000Error: No answer to stdout. Died or killed?
82Oct 15, 2024 21:1300000Error: expected "1216922 ", got "1221562 "
83Oct 15, 2024 20:1763,61261,802,08732,907,0003,988,0002,359,296
84Oct 15, 2024 20:1763,88463,360,93633,048,0004,005,0002,469,888
85Oct 15, 2024 20:1664,27962,948,89235,267,0002,015,0002,461,696
86Oct 15, 2024 20:1064,36960,463,33635,316,0002,018,0002,457,600
87Oct 15, 2024 20:1664,62866,052,58833,432,0004,052,0002,318,336
88Oct 15, 2024 20:1664,69059,709,51231,436,0006,084,0002,506,752
89Oct 15, 2024 20:1764,81766,298,82534,546,0003,048,0002,342,912
90Oct 15, 2024 20:1065,31464,090,69334,892,0002,990,0002,514,944
91Oct 15, 2024 20:1065,38663,188,07834,930,0002,994,0002,273,280
92Oct 15, 2024 20:0865,83467,936,22035,170,0003,014,0002,400,256
93Oct 15, 2024 20:0865,87167,194,93735,189,0003,016,0002,420,736
94Oct 15, 2024 20:2165,89561,995,53936,208,0002,011,0002,359,296
95Oct 15, 2024 20:2166,20060,407,43236,376,0002,020,0002,465,792
96Oct 15, 2024 20:2166,23860,946,42235,385,0003,033,0002,461,696
97Oct 15, 2024 21:1366,35967,034,64935,450,0003,038,0002,289,664
98Oct 15, 2024 20:0866,40763,483,89034,462,0004,054,0002,465,792
99Oct 15, 2024 20:0866,70566,935,83336,653,0002,036,0002,433,024
100Oct 15, 2024 20:0866,77669,610,99935,673,0003,057,0002,465,792
101Oct 15, 2024 21:1367,01765,638,44236,877,0001,993,0002,461,696
102Oct 15, 2024 21:1367,04572,057,75434,898,0003,988,0002,457,600
103Oct 15, 2024 20:0867,05769,048,77136,899,0001,994,0002,506,752
104Oct 15, 2024 20:0867,10263,355,12035,926,0002,993,0002,465,792
105Oct 15, 2024 20:2167,14866,837,33836,949,0001,997,0002,506,752
106Oct 15, 2024 21:1367,15763,124,31736,954,0001,997,0002,314,240
107Oct 15, 2024 21:1367,21661,028,96935,987,0002,998,0002,265,088
108Oct 15, 2024 20:0867,29873,115,10037,032,0002,001,0002,469,888
109Oct 15, 2024 20:2167,35061,291,75136,059,0003,004,0002,523,136
110Oct 15, 2024 21:1367,43165,432,71138,108,0001,002,0002,469,888
111Oct 15, 2024 20:0867,47161,241,38335,120,0004,013,0002,433,024
112Oct 15, 2024 20:2167,47862,148,69137,130,0002,007,0002,469,888
113Oct 15, 2024 20:2167,99573,417,64035,393,0004,044,0002,465,792
114Oct 15, 2024 20:2168,89368,569,78836,962,0002,996,0002,301,952
115Oct 15, 2024 20:2168,98163,077,40137,009,0003,000,0002,412,544