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 16, 2024 14:16@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 16, 2024 14:1700000Error: expected "976330 ", got "991922 "
2Oct 16, 2024 14:3500000Error: expected "2003116 ", got "2006401 "
3Oct 16, 2024 14:3500000Error: expected "1213522 ", got "1223122 "
4Oct 16, 2024 14:3500000Error: No answer to stdout. Died or killed?
5Oct 16, 2024 14:3500000Error: expected "1716394 ", got "1739426 "
6Oct 16, 2024 14:1700000Error: expected "714122 ", got "728049 "
7Oct 16, 2024 14:1700000Error: expected "2085620 ", got "2105857 "
8Oct 16, 2024 14:1700000Error: expected "1856210 ", got "1982057 "
9Oct 16, 2024 14:1700000Error: expected "684530 ", got "716428 "
10Oct 16, 2024 14:3500000Error: expected "1099871 ", got "1100066 "
11Oct 16, 2024 14:1700000Error: expected "793040 ", got "795321 "
12Oct 16, 2024 14:1700000Error: expected "1577778 ", got "1606820 "
13Oct 16, 2024 14:1700000Error: No answer to stdout. Died or killed?
14Oct 16, 2024 14:1700000Error: expected "2078268 ", got "2135867 "
15Oct 16, 2024 14:1700000Error: expected "1712648 ", got "1733735 "
16Oct 16, 2024 14:1600000Error: expected "1367139 ", got "1368478 "
17Oct 16, 2024 14:1600000Error: expected "1425373 ", got "1426725 "
18Oct 16, 2024 14:1600000Error: expected "1318637 ", got "2259324 "
19Oct 16, 2024 14:3500000Error: expected "1497090 ", got "1537619 "
20Oct 16, 2024 14:3600000Error: expected "2701153 ", got "2771711 "
21Oct 16, 2024 14:3700000Error: expected "1028287 ", got "1031896 "
22Oct 16, 2024 14:3800000Error: expected "849152 ", got "848862 "
23Oct 16, 2024 14:3800000Error: expected "719144 ", got "720760 "
24Oct 16, 2024 14:3800000Error: expected "1755124 ", got "1762213 "
25Oct 16, 2024 14:3800000Error: expected "2996077 ", got "2998463 "
26Oct 16, 2024 14:3800000Error: expected "1001211 ", got "1015958 "
27Oct 16, 2024 14:3800000Error: expected "1806634 ", got "1821140 "
28Oct 16, 2024 14:3800000Error: expected "572568 ", got "575268 "
29Oct 17, 2024 08:5900000Error: expected "1512992 ", got "1548382 "
30Oct 17, 2024 08:5900000Error: expected "1949000 ", got "1952777 "
31Oct 17, 2024 08:5900000Error: expected "1544488 ", got "1574560 "
32Oct 17, 2024 08:5900000Error: expected "2002321 ", got "2003502 "
33Oct 17, 2024 08:5900000Error: expected "746168 ", got "756483 "
34Oct 16, 2024 14:1600000Error: expected "798425 ", got "798817 "
35Oct 16, 2024 14:1600000Error: expected "1819153 ", got "1819762 "
36Oct 16, 2024 14:1600000Error: expected "2195249 ", got "2292554 "
37Oct 16, 2024 14:1600000Error: expected "2894219 ", got "2914241 "
38Oct 16, 2024 14:1600000Error: expected "1610212 ", got "1714147 "
39Oct 16, 2024 14:1645,23148,448,43722,198,0004,036,0002,359,296
40Oct 16, 2024 14:3745,40747,321,50223,298,0003,038,0002,322,432
41Oct 16, 2024 14:3545,50548,587,67625,378,0001,015,0002,330,624
42Oct 16, 2024 14:3545,51051,597,87623,351,0003,045,0002,281,472
43Oct 17, 2024 08:5945,53354,178,43723,362,0003,047,0002,465,792
44Oct 16, 2024 14:3845,55048,247,69724,387,0002,032,0002,465,792
45Oct 16, 2024 14:1645,64147,626,34724,436,0002,036,0002,469,888
46Oct 16, 2024 14:1645,66750,240,89124,450,0002,037,0002,289,664
47Oct 16, 2024 14:1645,68347,746,36723,439,0003,057,0002,465,792
48Oct 16, 2024 14:1645,73453,853,66824,486,0002,040,0002,449,408
49Oct 16, 2024 14:1645,74756,473,31021,431,0005,102,0002,289,664
50Oct 16, 2024 14:1745,76651,527,33824,503,0002,041,0002,449,408
51Oct 16, 2024 14:1745,91253,295,15125,605,0001,024,0002,514,944
52Oct 17, 2024 08:5945,94555,079,61523,574,0003,074,0002,469,888
53Oct 17, 2024 08:5945,96456,131,58625,634,0001,025,0002,400,256
54Oct 16, 2024 14:3845,98356,088,01325,645,0001,025,0002,293,760
55Oct 16, 2024 14:1745,98653,598,77324,621,0002,051,0002,461,696
56Oct 16, 2024 14:1646,23853,882,01122,845,0003,973,0002,273,280
57Oct 16, 2024 14:1646,28153,709,36023,861,0002,982,0002,338,816
58Oct 16, 2024 14:3746,35358,171,31123,898,0002,987,0002,473,984
59Oct 16, 2024 14:1646,35953,242,82526,888,00002,281,472
60Oct 16, 2024 14:1646,36654,072,73322,908,0003,984,0002,478,080
61Oct 16, 2024 14:3846,36753,566,01924,901,0001,992,0002,478,080
62Oct 16, 2024 14:3746,38852,463,82225,909,000996,0002,383,872
63Oct 16, 2024 14:1646,40749,608,51023,926,0002,990,0002,453,504
64Oct 16, 2024 14:3546,49348,892,09824,969,0001,997,0002,371,584
65Oct 16, 2024 14:1647,13350,417,67424,300,0003,037,0002,330,624