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 14:00@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 14:0100000Error: expected "2072139 ", got "2070169 "
2Oct 17, 2024 14:0000000Error: expected "873261 ", got "875474 "
3Oct 17, 2024 14:0000000Error: expected "1322376 ", got "1322134 "
4Oct 17, 2024 14:0000000Error: expected "1171234 ", got "1171362 "
5Oct 17, 2024 14:0100000Error: expected "1774711 ", got "1774776 "
6Oct 17, 2024 14:0100000Error: expected "1279263 ", got "1278300 "
7Oct 17, 2024 14:0100000Error: expected "1957443 ", got "1966767 "
8Oct 17, 2024 14:0100000Error: expected "864541 ", got "864964 "
9Oct 17, 2024 14:0100000Error: expected "1336782 ", got "1332145 "
10Oct 17, 2024 14:0100000Error: expected "1276520 ", got "1275879 "
11Oct 17, 2024 14:0100000Error: expected "2090792 ", got "2090942 "
12Oct 17, 2024 14:0100000Error: expected "1404552 ", got "1437929 "
13Oct 17, 2024 14:0000000Error: expected "2482338 ", got "2485202 "
14Oct 17, 2024 14:0100000Error: expected "1162717 ", got "1162544 "
15Oct 17, 2024 14:0100000Error: expected "1216585 ", got "1217158 "
16Oct 17, 2024 14:0100000Error: expected "761672 ", got "761762 "
17Oct 17, 2024 14:0100000Error: expected "1375682 ", got "1375702 "
18Oct 17, 2024 14:0100000Error: expected "1302426 ", got "1296243 "
19Oct 17, 2024 14:0100000Error: expected "960147 ", got "965099 "
20Oct 17, 2024 14:0100000Error: expected "2481757 ", got "2481815 "
21Oct 17, 2024 14:0100000Error: expected "518845 ", got "519427 "
22Oct 17, 2024 14:0100000Error: expected "1347636 ", got "1346886 "
23Oct 17, 2024 14:0100000Error: expected "1301399 ", got "1301084 "
24Oct 17, 2024 14:0000000Error: expected "1898172 ", got "1897269 "
25Oct 17, 2024 14:0000000Error: expected "1077739 ", got "1078553 "
26Oct 17, 2024 14:0000000Error: expected "2321447 ", got "2323981 "
27Oct 17, 2024 14:0000000Error: expected "2140603 ", got "2145025 "
28Oct 17, 2024 14:0000000Error: expected "2442684 ", got "2441464 "
29Oct 17, 2024 14:0000000Error: expected "721827 ", got "721126 "
30Oct 17, 2024 14:0000000Error: expected "701849 ", got "704302 "
31Oct 17, 2024 14:0000000Error: expected "1332662 ", got "1332797 "
32Oct 17, 2024 14:0000000Error: expected "1320759 ", got "1319957 "
33Oct 17, 2024 14:0000000Error: expected "1737522 ", got "1729331 "
34Oct 17, 2024 14:0000000Error: expected "924262 ", got "922900 "
35Oct 17, 2024 14:0000000Error: expected "1303890 ", got "1305014 "
36Oct 17, 2024 14:0000000Error: expected "2070030 ", got "2072182 "
37Oct 17, 2024 14:0000000Error: expected "1855828 ", got "1854392 "
38Oct 17, 2024 14:0000000Error: expected "1733439 ", got "1733399 "
39Oct 17, 2024 14:0000000Error: expected "1508404 ", got "1575598 "
40Oct 17, 2024 14:0000000Error: expected "1118934 ", got "1118089 "
41Oct 17, 2024 14:0000000Error: expected "1581202 ", got "1583994 "
42Oct 17, 2024 14:0000000Error: expected "753646 ", got "785422 "
43Oct 17, 2024 14:0018,24129,582,2057,406,0003,174,0002,465,792
44Oct 17, 2024 14:0018,86726,511,7299,949,000994,0002,465,792
45Oct 17, 2024 14:0019,88433,337,6459,436,0002,097,0002,473,984