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 13:12@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 13:1300000Error: No answer to stdout. Died or killed?
2Oct 17, 2024 13:1300000Error: expected "800853 ", got "821833 "
3Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
4Oct 17, 2024 13:1300000Error: expected "803426 ", got "903184 "
5Oct 17, 2024 13:1300000Error: expected "1501679 ", got "1501187 "
6Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
7Oct 17, 2024 13:1300000Error: expected "1399642 ", got "1443913 "
8Oct 17, 2024 13:1300000Error: expected "2300560 ", got "2416124 "
9Oct 17, 2024 13:1300000Error: expected "1195964 ", got "1195343 "
10Oct 17, 2024 13:1300000Error: expected "1898289 ", got "1895761 "
11Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
12Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
13Oct 17, 2024 13:1300000Error: expected "710436 ", got "712560 "
14Oct 17, 2024 13:1300000Error: expected "1163064 ", got "1164798 "
15Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
16Oct 17, 2024 13:1300000Error: expected "1026859 ", got "6778430 "
17Oct 17, 2024 13:1300000Error: expected "1141518 ", got "1138370 "
18Oct 17, 2024 13:1400000Error: No answer to stdout. Died or killed?
19Oct 17, 2024 13:1400000Error: No answer to stdout. Died or killed?
20Oct 17, 2024 13:1400000Error: expected "971362 ", got "1001270 "
21Oct 17, 2024 13:1400000Error: expected "3157235 ", got "3160475 "
22Oct 17, 2024 13:1400000Error: No answer to stdout. Died or killed?
23Oct 17, 2024 13:1400000Error: expected "855742 ", got "864281 "
24Oct 17, 2024 13:1400000Error: expected "1650117 ", got "1655213 "
25Oct 17, 2024 13:1400000Error: No answer to stdout. Died or killed?
26Oct 17, 2024 13:1400000Error: expected "1422002 ", got "1419551 "
27Oct 17, 2024 13:1400000Error: No answer to stdout. Died or killed?
28Oct 17, 2024 13:1400000Error: expected "837112 ", got "847407 "
29Oct 17, 2024 13:1400000Error: expected "2670909 ", got "2689777 "
30Oct 17, 2024 13:1400000Error: expected "895678 ", got "902001 "
31Oct 17, 2024 13:1200000Error: No answer to stdout. Died or killed?
32Oct 17, 2024 13:1200000Error: expected "1099539 ", got "1100897 "
33Oct 17, 2024 13:1200000Error: No answer to stdout. Died or killed?
34Oct 17, 2024 13:1200000Error: expected "964261 ", got "965006 "
35Oct 17, 2024 13:1200000Error: expected "1267361 ", got "1276513 "
36Oct 17, 2024 13:1200000Error: No answer to stdout. Died or killed?
37Oct 17, 2024 13:1200000Error: expected "1480837 ", got "1490486 "
38Oct 17, 2024 13:1200000Error: No answer to stdout. Died or killed?
39Oct 17, 2024 13:1200000Error: No answer to stdout. Died or killed?
40Oct 17, 2024 13:1200000Error: expected "763151 ", got "765500 "
41Oct 17, 2024 13:1200000Error: expected "1913710 ", got "1940190 "
42Oct 17, 2024 13:1200000Error: expected "3135860 ", got "3731403 "
43Oct 17, 2024 13:1200000Error: No answer to stdout. Died or killed?
44Oct 17, 2024 13:1200000Error: expected "1487515 ", got "1548235 "
45Oct 17, 2024 13:1200000Error: expected "2201930 ", got "2211464 "
46Oct 17, 2024 13:1200000Error: expected "2524287 ", got "2560030 "
47Oct 17, 2024 13:1200000Error: expected "2803598 ", got "2804579 "
48Oct 17, 2024 13:1200000Error: No answer to stdout. Died or killed?
49Oct 17, 2024 13:1200000Error: expected "769655 ", got "769669 "
50Oct 17, 2024 13:1200000Error: expected "2852101 ", got "2860342 "
51Oct 17, 2024 13:1200000Error: expected "1901546 ", got "2621628 "
52Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
53Oct 17, 2024 13:1300000Error: expected "1661975 ", got "1677600 "
54Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
55Oct 17, 2024 13:1300000Error: No answer to stdout. Died or killed?
56Oct 17, 2024 13:1300000Error: expected "1835306 ", got "1967214 "
57Oct 17, 2024 13:1300000Error: expected "1645268 ", got "1645988 "
58Oct 17, 2024 13:1300000Error: expected "1905704 ", got "1924565 "
59Oct 17, 2024 13:1300000Error: expected "1389207 ", got "1671894 "