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 17, 2024 11:07@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 17, 2024 11:0700000Error: expected "1598323 ", got "1863895 "
2Oct 17, 2024 11:0800000Error: expected "1804881 ", got "1814965 "
3Oct 17, 2024 11:0800000Error: expected "890303 ", got "5282479 "
4Oct 17, 2024 11:0800000Error: expected "955957 ", got "958837 "
5Oct 17, 2024 11:0800000Error: expected "1338295 ", got "1364598 "
6Oct 17, 2024 11:0800000Error: expected "1307075 ", got "1309703 "
7Oct 17, 2024 11:0800000Error: expected "1341126 ", got "1353323 "
8Oct 17, 2024 11:0800000Error: expected "1160575 ", got "1311611 "
9Oct 17, 2024 11:0800000Error: No answer to stdout. Died or killed?
10Oct 17, 2024 11:0800000Error: expected "1039583 ", got "1039591 "
11Oct 17, 2024 11:0800000Error: No answer to stdout. Died or killed?
12Oct 17, 2024 11:0800000Error: expected "1857026 ", got "1887107 "
13Oct 17, 2024 11:0800000Error: expected "1801276 ", got "2027521 "
14Oct 17, 2024 11:0800000Error: expected "674747 ", got "697206 "
15Oct 17, 2024 11:0800000Error: expected "1194436 ", got "1247928 "
16Oct 17, 2024 11:0800000Error: expected "683245 ", got "694107 "
17Oct 17, 2024 11:0800000Error: expected "885370 ", got "888992 "
18Oct 17, 2024 11:0800000Error: expected "2060067 ", got "2300087 "
19Oct 17, 2024 11:0800000Error: expected "750837 ", got "751994 "
20Oct 17, 2024 11:0800000Error: expected "660031 ", got "661012 "
21Oct 17, 2024 11:0800000Error: expected "2036805 ", got "2053355 "
22Oct 17, 2024 11:0800000Error: expected "681047 ", got "681250 "
23Oct 17, 2024 11:0800000Error: expected "1667861 ", got "1668598 "
24Oct 17, 2024 11:0800000Error: expected "1149016 ", got "1158893 "
25Oct 17, 2024 11:0800000Error: expected "3149936 ", got "3255278 "
26Oct 17, 2024 11:0800000Error: expected "1128281 ", got "1148139 "
27Oct 17, 2024 11:0800000Error: expected "965189 ", got "973091 "
28Oct 17, 2024 11:0900000Error: expected "709402 ", got "720416 "
29Oct 17, 2024 11:1900000Error: expected "1373282 ", got "1495434 "
30Oct 17, 2024 11:1900000Error: expected "660856 ", got "669834 "
31Oct 17, 2024 11:2000000Error: expected "1165726 ", got "1233755 "
32Oct 17, 2024 11:2000000Error: expected "912263 ", got "1238060 "
33Oct 17, 2024 11:2000000Error: expected "1656291 ", got "1750541 "
34Oct 17, 2024 11:2000000Error: expected "1730926 ", got "1961701 "
35Oct 17, 2024 11:2000000Error: expected "2386969 ", got "2460398 "
36Oct 17, 2024 11:2000000Error: expected "1622564 ", got "1668406 "
37Oct 17, 2024 11:2000000Error: expected "1506920 ", got "1592200 "
38Oct 17, 2024 11:2000000Error: expected "748526 ", got "775519 "
39Oct 17, 2024 11:2000000Error: expected "2990711 ", got "3000466 "
40Oct 17, 2024 11:2000000Error: expected "1285903 ", got "1290392 "
41Oct 17, 2024 11:2000000Error: No answer to stdout. Died or killed?
42Oct 17, 2024 11:2000000Error: expected "1063626 ", got "1356099 "
43Oct 17, 2024 11:2000000Error: expected "2112538 ", got "2151529 "
44Oct 17, 2024 11:2000000Error: expected "1036111 ", got "1121657 "
45Oct 17, 2024 11:2000000Error: expected "1547790 ", got "2036680 "
46Oct 17, 2024 11:2000000Error: expected "1638279 ", got "1704693 "
47Oct 17, 2024 11:2000000Error: expected "1574539 ", got "1724674 "
48Oct 17, 2024 11:2000000Error: expected "2602200 ", got "2624878 "
49Oct 17, 2024 11:2200000Error: expected "673883 ", got "672812 "
50Oct 17, 2024 11:2200000Error: expected "2706379 ", got "2797380 "
51Oct 17, 2024 11:2200000Error: expected "1378872 ", got "1440338 "
52Oct 17, 2024 11:2200000Error: expected "949029 ", got "956577 "
53Oct 17, 2024 11:2300000Error: expected "1085301 ", got "1091084 "
54Oct 17, 2024 11:2300000Error: expected "2817713 ", got "2866164 "
55Oct 17, 2024 11:2300000Error: expected "1972193 ", got "1972363 "
56Oct 17, 2024 11:2300000Error: expected "1811909 ", got "2261573 "
57Oct 17, 2024 11:2300000Error: expected "697399 ", got "728768 "
58Oct 17, 2024 11:2300000Error: expected "1206859 ", got "1210708 "
59Oct 17, 2024 11:2300000Error: expected "1301792 ", got "1386610 "
60Oct 17, 2024 11:2300000Error: expected "2828241 ", got "2838470 "
61Oct 17, 2024 11:2300000Error: expected "1098813 ", got "1099596 "
62Oct 17, 2024 11:2300000Error: expected "2825563 ", got "2865196 "
63Oct 17, 2024 11:2300000Error: expected "1566640 ", got "1575937 "
64Oct 17, 2024 11:2300000Error: expected "1706127 ", got "1708750 "
65Oct 17, 2024 11:2300000Error: expected "2396030 ", got "2441961 "
66Oct 17, 2024 11:2300000Error: expected "1480045 ", got "1507008 "
67Oct 17, 2024 11:2300000Error: expected "618438 ", got "1029728 "
68Oct 17, 2024 11:2300000Error: expected "2239617 ", got "2339629 "
69Oct 17, 2024 11:2300000Error: expected "720016 ", got "726334 "
70Oct 17, 2024 11:2300000Error: expected "2639690 ", got "2646339 "
71Oct 17, 2024 11:2300000Error: expected "1408865 ", got "1415560 "
72Oct 17, 2024 11:2300000Error: expected "2318695 ", got "2384658 "
73Oct 17, 2024 11:2300000Error: expected "3839905 ", got "3845131 "
74Oct 17, 2024 11:2300000Error: expected "768065 ", got "764824 "
75Oct 17, 2024 11:2300000Error: expected "2721939 ", got "2722128 "
76Oct 17, 2024 11:2400000Error: expected "1387603 ", got "4791050 "
77Oct 17, 2024 11:2400000Error: No answer to stdout. Died or killed?
78Oct 17, 2024 11:2400000Error: expected "2003739 ", got "2003777 "
79Oct 17, 2024 11:2400000Error: expected "699419 ", got "829650 "
80Oct 17, 2024 11:2400000Error: expected "2455463 ", got "2560961 "
81Oct 17, 2024 11:2400000Error: expected "795891 ", got "814162 "
82Oct 17, 2024 11:2400000Error: expected "2137889 ", got "2142435 "
83Oct 17, 2024 11:2400000Error: expected "1929522 ", got "1929567 "
84Oct 17, 2024 11:2400000Error: expected "2149610 ", got "2150072 "
85Oct 17, 2024 11:2400000Error: expected "991895 ", got "3553442 "
86Oct 17, 2024 11:2400000Error: expected "985418 ", got "1016819 "
87Oct 17, 2024 11:2400000Error: expected "1647030 ", got "1651788 "
88Oct 17, 2024 11:2400000Error: No answer to stdout. Died or killed?
89Oct 17, 2024 11:2400000Error: expected "925474 ", got "950735 "
90Oct 17, 2024 11:2400000Error: expected "1844589 ", got "5513102 "
91Oct 17, 2024 11:2400000Error: expected "1474054 ", got "1517418 "
92Oct 17, 2024 11:2400000Error: expected "733073 ", got "725398 "
93Oct 17, 2024 11:2400000Error: expected "1017379 ", got "1018864 "
94Oct 17, 2024 11:2400000Error: expected "2493095 ", got "2493101 "
95Oct 17, 2024 11:2400000Error: expected "629284 ", got "628159 "
96Oct 17, 2024 11:2400000Error: expected "630671 ", got "4800095 "
97Oct 17, 2024 11:2400000Error: expected "727149 ", got "744592 "
98Oct 17, 2024 11:2400000Error: expected "2356305 ", got "2516584 "
99Oct 17, 2024 11:2343,59056,178,58323,260,0002,022,0002,437,120
100Oct 17, 2024 11:2343,64348,486,18423,288,0002,025,0002,277,376
101Oct 17, 2024 11:2343,69746,862,13621,289,0004,055,0002,465,792
102Oct 17, 2024 11:2343,71051,126,72623,324,0002,028,0002,469,888
103Oct 17, 2024 11:2343,75758,197,78321,319,0004,060,0002,502,656
104Oct 17, 2024 11:2343,81753,296,58723,381,0002,033,0002,293,760
105Oct 17, 2024 11:2344,03850,066,24622,477,0003,065,0002,469,888
106Oct 17, 2024 11:2344,34351,471,23222,633,0003,086,0002,281,472
107Oct 17, 2024 11:2346,44050,227,02323,943,0002,992,0002,269,184