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 12: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 12:1200000Error: expected "2898513 ", got "2898525 "
2Oct 17, 2024 12:1300000Error: expected "1592128 ", got "1782569 "
3Oct 17, 2024 12:1300000Error: expected "1929660 ", got "2088284 "
4Oct 17, 2024 12:1300000Error: expected "2288054 ", got "2289594 "
5Oct 17, 2024 12:1300000Error: expected "708311 ", got "710478 "
6Oct 17, 2024 12:1300000Error: expected "1771391 ", got "1875586 "
7Oct 17, 2024 12:1300000Error: expected "1853545 ", got "1851663 "
8Oct 17, 2024 12:1300000Error: No answer to stdout. Died or killed?
9Oct 17, 2024 12:1300000Error: expected "2034191 ", got "2059237 "
10Oct 17, 2024 12:1300000Error: expected "747881 ", got "747683 "
11Oct 17, 2024 12:1300000Error: expected "672110 ", got "672209 "
12Oct 17, 2024 12:1200000Error: expected "860106 ", got "903676 "
13Oct 17, 2024 12:1300000Error: expected "1277102 ", got "1296555 "
14Oct 17, 2024 12:1100000Error: expected "2107003 ", got "2107018 "
15Oct 17, 2024 12:1100000Error: expected "2242893 ", got "2254380 "
16Oct 17, 2024 12:1100000Error: expected "934599 ", got "934986 "
17Oct 17, 2024 12:1100000Error: expected "2577470 ", got "2595103 "
18Oct 17, 2024 12:1100000Error: expected "1958696 ", got "1961168 "
19Oct 17, 2024 12:1100000Error: expected "1879634 ", got "1891080 "
20Oct 17, 2024 12:1100000Error: expected "1766619 ", got "1888102 "
21Oct 17, 2024 12:1100000Error: expected "879120 ", got "907547 "
22Oct 17, 2024 12:1100000Error: expected "633414 ", got "633999 "
23Oct 17, 2024 12:1100000Error: expected "1743969 ", got "1749864 "
24Oct 17, 2024 12:1400000Error: expected "1200688 ", got "1344091 "
25Oct 17, 2024 12:4000000Error: expected "1874206 ", got "1916261 "
26Oct 17, 2024 12:4000000Error: expected "2094192 ", got "2100245 "
27Oct 17, 2024 12:3900000Error: expected "649992 ", got "655372 "
28Oct 17, 2024 12:3900000Error: expected "1491246 ", got "1534073 "
29Oct 17, 2024 12:3900000Error: expected "1054672 ", got "1055820 "
30Oct 17, 2024 12:3900000Error: expected "1858252 ", got "1871886 "
31Oct 17, 2024 12:3900000Error: expected "872806 ", got "927228 "
32Oct 17, 2024 12:3900000Error: expected "801977 ", got "1103815 "
33Oct 17, 2024 12:3900000Error: expected "1091100 ", got "1092432 "
34Oct 17, 2024 12:1500000Error: expected "1241114 ", got "1239692 "
35Oct 17, 2024 12:1500000Error: expected "1575241 ", got "1581880 "
36Oct 17, 2024 12:1100000Error: expected "1244236 ", got "1452492 "
37Oct 17, 2024 12:1400000Error: expected "632639 ", got "631845 "
38Oct 17, 2024 12:1400000Error: expected "2542112 ", got "2897917 "
39Oct 17, 2024 12:1400000Error: expected "851162 ", got "853404 "
40Oct 17, 2024 12:1400000Error: expected "1064331 ", got "1064538 "
41Oct 17, 2024 12:1400000Error: expected "1685113 ", got "1683764 "
42Oct 17, 2024 12:1400000Error: expected "1564536 ", got "1564602 "
43Oct 17, 2024 12:1400000Error: expected "1443989 ", got "1480763 "
44Oct 17, 2024 12:1400000Error: expected "1383954 ", got "1390029 "
45Oct 17, 2024 12:1300000Error: expected "1837268 ", got "1838959 "
46Oct 17, 2024 12:1300000Error: expected "790968 ", got "807924 "
47Oct 17, 2024 12:0800000Error: expected "1318945 ", got "1468980 "
48Oct 17, 2024 12:0900000Error: expected "2888307 ", got "2898747 "
49Oct 17, 2024 12:0900000Error: expected "934040 ", got "935062 "
50Oct 17, 2024 12:0900000Error: expected "1243818 ", got "1305509 "
51Oct 17, 2024 12:0800000Error: expected "1334957 ", got "1364481 "
52Oct 17, 2024 12:0800000Error: expected "1569425 ", got "1573553 "
53Oct 17, 2024 12:0800000Error: expected "916332 ", got "936544 "
54Oct 17, 2024 12:0800000Error: expected "1698256 ", got "1784118 "
55Oct 17, 2024 12:0800000Error: expected "1320947 ", got "1337501 "
56Oct 17, 2024 12:0800000Error: expected "783650 ", got "995753 "
57Oct 17, 2024 12:0800000Error: expected "1034929 ", got "1068835 "
58Oct 17, 2024 12:1100000Error: expected "1754900 ", got "1772807 "
59Oct 17, 2024 12:0700000Error: expected "2514808 ", got "2569174 "
60Oct 17, 2024 12:0700000Error: expected "2455413 ", got "5219640 "
61Oct 17, 2024 12:0700000Error: expected "1347829 ", got "1372083 "
62Oct 17, 2024 12:0700000Error: expected "1918608 ", got "1923682 "
63Oct 17, 2024 12:0700000Error: expected "1818910 ", got "1825724 "
64Oct 17, 2024 12:0700000Error: expected "1195069 ", got "1195501 "
65Oct 17, 2024 12:0700000Error: expected "1759368 ", got "1960021 "
66Oct 17, 2024 12:0700000Error: expected "791235 ", got "807979 "
67Oct 17, 2024 12:0700000Error: expected "2301046 ", got "2373609 "
68Oct 17, 2024 12:0700000Error: expected "2629995 ", got "2630202 "
69Oct 17, 2024 12:0900000Error: expected "1260731 ", got "1301104 "
70Oct 17, 2024 12:1100000Error: expected "1131807 ", got "1134351 "
71Oct 17, 2024 12:1100000Error: expected "1037386 ", got "1036980 "
72Oct 17, 2024 12:1100000Error: expected "1486308 ", got "1539520 "
73Oct 17, 2024 12:1100000Error: expected "557756 ", got "559871 "
74Oct 17, 2024 12:1100000Error: expected "2767524 ", got "2881262 "
75Oct 17, 2024 12:1000000Error: expected "766735 ", got "813268 "
76Oct 17, 2024 12:1000000Error: No answer to stdout. Died or killed?
77Oct 17, 2024 12:1000000Error: expected "1428100 ", got "1674117 "
78Oct 17, 2024 12:1000000Error: expected "1058099 ", got "1064431 "
79Oct 17, 2024 12:1000000Error: expected "1052695 ", got "1052095 "
80Oct 17, 2024 12:1000000Error: expected "1847464 ", got "1935812 "
81Oct 17, 2024 12:1000000Error: expected "730106 ", got "733868 "
82Oct 17, 2024 12:1000000Error: expected "916691 ", got "1076553 "
83Oct 17, 2024 12:0900000Error: expected "1133226 ", got "1169135 "
84Oct 17, 2024 12:0900000Error: expected "1738474 ", got "1801102 "
85Oct 17, 2024 12:0900000Error: expected "1611252 ", got "1740890 "
86Oct 17, 2024 12:0900000Error: expected "1398922 ", got "1473050 "
87Oct 17, 2024 12:0900000Error: expected "1384132 ", got "1383523 "
88Oct 17, 2024 12:0900000Error: expected "859681 ", got "982068 "
89Oct 17, 2024 12:0900000Error: expected "933233 ", got "1025734 "
90Oct 17, 2024 12:1439,45342,316,07818,904,0003,979,0002,461,696
91Oct 17, 2024 12:1439,88362,715,71721,121,0002,011,0002,469,888
92Oct 17, 2024 12:1441,96750,849,85021,299,0003,042,0002,265,088