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 15, 2024 19:42@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 15, 2024 20:0800000Error: expected "693937 ", got "694203 "
2Oct 15, 2024 19:5700000Error: expected "1169096 ", got "1172302 "
3Oct 15, 2024 19:5200000Error: expected "1968840 ", got "1971567 "
4Oct 15, 2024 19:5200000Error: No answer to stdout. Died or killed?
5Oct 15, 2024 19:5200000Error: No answer to stdout. Died or killed?
6Oct 15, 2024 19:5264,07162,405,37533,144,0004,017,0002,301,952
7Oct 15, 2024 19:5264,34759,710,31734,295,0003,026,0002,469,888
8Oct 15, 2024 19:5265,74061,570,04236,123,0002,006,0002,469,888
9Oct 15, 2024 20:0866,59063,075,90237,606,0001,016,0002,461,696
10Oct 15, 2024 20:0867,05763,778,85934,904,0003,989,0002,301,952
11Oct 15, 2024 20:0867,07267,077,75034,912,0003,990,0002,363,392
12Oct 15, 2024 20:0867,11670,744,99534,935,0003,992,0002,453,504
13Oct 15, 2024 20:0867,20063,821,18834,979,0003,997,0002,469,888
14Oct 15, 2024 20:0867,25361,534,43737,007,0002,000,0002,473,984
15Oct 15, 2024 20:0867,29862,825,87036,031,0003,002,0002,322,432
16Oct 15, 2024 20:0867,30565,322,52236,035,0003,002,0002,469,888
17Oct 15, 2024 20:0867,50563,067,19636,142,0003,011,0002,281,472
18Oct 15, 2024 20:0867,56661,546,36336,174,0003,014,0002,404,352
19Oct 15, 2024 20:0867,82872,671,18834,297,0005,043,0002,465,792
20Oct 15, 2024 20:0868,00068,516,79935,395,0004,045,0002,465,792
21Oct 15, 2024 19:5769,77261,754,42438,445,0002,023,0002,469,888
22Oct 15, 2024 19:5769,95964,488,86140,576,00002,473,984
23Oct 15, 2024 19:5770,02969,227,52138,587,0002,030,0002,510,848
24Oct 15, 2024 19:4270,21472,916,90337,670,0003,054,0002,355,200
25Oct 15, 2024 19:4270,25374,000,55639,729,0001,018,0002,465,792
26Oct 15, 2024 19:5770,50767,310,98438,900,0001,994,0002,269,184
27Oct 15, 2024 19:4270,50970,267,34938,901,0001,994,0002,351,104
28Oct 15, 2024 19:4270,52470,622,58338,909,0001,995,0002,514,944
29Oct 15, 2024 19:4270,57971,424,72738,940,0001,996,0002,523,136
30Oct 15, 2024 19:4270,82872,767,11137,073,0004,007,0002,375,680
31Oct 15, 2024 19:5770,91470,170,04239,124,0002,006,0002,306,048
32Oct 15, 2024 19:4271,00968,978,90736,163,0005,022,0002,478,080
33Oct 15, 2024 19:4271,19563,718,43238,272,0003,021,0002,510,848
34Oct 15, 2024 19:5771,30966,921,96937,324,0004,035,0002,428,928
35Oct 15, 2024 19:4271,37966,247,08737,361,0004,039,0002,289,664