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 listDec 27, 2024 02:49Joad NacerJoad NacerError
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 27, 2024 02:5300000Error: expected "1305123 ", got "1077000 "stderr
2Dec 27, 2024 02:5400000Error: expected "842890 ", got "807961 "stderr
3Dec 27, 2024 02:5400000Error: expected "3608366 ", got "3600591 "stderr
4Dec 27, 2024 02:5400000Error: expected "1157242 ", got "1136173 "stderr
5Dec 27, 2024 02:5400000Error: expected "2456994 ", got "2456910 "stderr
6Dec 27, 2024 02:5400000Error: expected "1830780 ", got "1789000 "stderr
7Dec 27, 2024 02:5300000Error: expected "807247 ", got "791714 "stderr
8Dec 27, 2024 02:5300000Error: expected "849295 ", got "809790 "stderr
9Dec 27, 2024 02:5300000Error: expected "1728712 ", got "1680664 "stderr
10Dec 27, 2024 02:5300000Error: expected "1950289 ", got "1566000 "stderr
11Dec 27, 2024 02:5400000Error: expected "1346564 ", got "1236900 "stderr
12Dec 27, 2024 02:5300000Error: expected "1968264 ", got "1904490 "stderr
13Dec 27, 2024 02:5200000Error: expected "2635212 ", got "2609709 "stderr
14Dec 27, 2024 02:5200000Error: expected "1530794 ", got "1418646 "stderr
15Dec 27, 2024 02:5200000Error: expected "748305 ", got "721454 "stderr
16Dec 27, 2024 02:5200000Error: expected "2729855 ", got "2684165 "stderr
17Dec 27, 2024 02:5200000Error: expected "1558904 ", got "1550185 "stderr
18Dec 27, 2024 02:5200000Error: expected "1872201 ", got "1722734 "stderr
19Dec 27, 2024 02:5200000Error: expected "715308 ", got "692593 "stderr
20Dec 27, 2024 02:5100000Error: expected "778732 ", got "774839 "stderr
21Dec 27, 2024 02:5600000Error: expected "2196612 ", got "2051000 "stderr
22Dec 27, 2024 02:5700000Error: expected "2101144 ", got "2097166 "stderr
23Dec 27, 2024 02:5700000Error: expected "954241 ", got "947523 "stderr
24Dec 27, 2024 02:5700000Error: expected "1716921 ", got "1665551 "stderr
25Dec 27, 2024 02:5700000Error: expected "2622411 ", got "2559718 "stderr
26Dec 27, 2024 02:5700000Error: expected "1467283 ", got "1439000 "stderr
27Dec 27, 2024 02:5600000Error: expected "972859 ", got "912184 "stderr
28Dec 27, 2024 02:5600000Error: expected "2056576 ", got "1908000 "stderr
29Dec 27, 2024 02:5600000Error: expected "1337322 ", got "1274000 "stderr
30Dec 27, 2024 02:5600000Error: expected "1172199 ", got "1083681 "stderr
31Dec 27, 2024 02:4900000Error: expected "1872164 ", got "1696646 "stderr
32Dec 27, 2024 02:5600000Error: expected "1944353 ", got "1923331 "stderr
33Dec 27, 2024 02:5600000Error: expected "1188319 ", got "1187829 "stderr
34Dec 27, 2024 02:5500000Error: expected "1661300 ", got "1646890 "stderr
35Dec 27, 2024 02:5500000Error: expected "1663723 ", got "1580883 "stderr
36Dec 27, 2024 02:5500000Error: expected "2971145 ", got "2943374 "stderr
37Dec 27, 2024 02:5500000Error: expected "1146052 ", got "1072494 "stderr
38Dec 27, 2024 02:5500000Error: expected "2072192 ", got "2063409 "stderr
39Dec 27, 2024 02:5400000Error: expected "3207699 ", got "3154000 "stderr
40Dec 27, 2024 02:5721,62231,668,52912,541,00002,338,816
41Dec 27, 2024 02:5622,83131,181,28412,224,0001,018,0002,510,848
42Dec 27, 2024 02:5224,26938,450,52014,076,00002,363,392
43Dec 27, 2024 02:5624,74830,468,76213,329,0001,025,0002,342,912
44Dec 27, 2024 02:5425,32135,516,03212,588,0002,098,0002,404,352
45Dec 27, 2024 02:5525,71436,234,14913,920,000994,0002,457,600
46Dec 27, 2024 02:5326,73640,335,58915,507,00002,342,912
47Dec 27, 2024 02:5726,76033,200,90415,521,00002,306,048
48Dec 27, 2024 02:5727,14536,715,89615,744,00002,461,696
49Dec 27, 2024 02:5528,29143,623,73116,409,00002,371,584
50Dec 27, 2024 02:5230,16039,887,65017,493,00002,342,912
51Dec 27, 2024 02:5630,20941,998,72716,491,0001,030,0002,506,752
52Dec 27, 2024 02:5130,24549,090,12916,511,0001,031,0002,355,200
53Dec 27, 2024 02:5731,82239,599,75118,457,00002,428,928
54Dec 27, 2024 02:5531,88350,343,39418,492,00002,330,624
55Dec 27, 2024 02:5133,01049,362,61918,139,0001,007,0002,330,624
56Dec 27, 2024 02:5433,50242,041,67019,431,00002,359,296
57Dec 27, 2024 02:5234,59844,126,28220,067,00002,359,296
58Dec 27, 2024 02:5534,64749,725,40120,095,00002,322,432
59Dec 27, 2024 02:5335,13649,140,05320,379,00002,428,928
60Dec 27, 2024 02:5535,23438,497,00220,436,00002,342,912
61Dec 27, 2024 02:5235,55242,148,51620,620,00002,338,816
62Dec 27, 2024 02:5735,71246,096,18420,713,00002,310,144
63Dec 27, 2024 02:5135,77252,275,23620,748,00002,355,200
64Dec 27, 2024 02:5236,19745,852,22119,995,000999,0002,387,968
65Dec 27, 2024 02:5536,24843,463,82419,022,0002,002,0002,359,296
66Dec 27, 2024 02:5336,31746,792,39021,064,00002,375,680
67Dec 27, 2024 02:5736,41747,663,43519,111,0002,011,0002,408,448
68Dec 27, 2024 02:5536,74047,018,20621,309,00002,400,256
69Dec 27, 2024 02:5736,96743,048,94621,441,00002,437,120
70Dec 27, 2024 02:5336,99139,893,25420,434,0001,021,0002,482,176
71Dec 27, 2024 02:5737,05340,193,40220,468,0001,023,0002,379,776
72Dec 27, 2024 02:5237,20045,823,23221,576,00002,445,312
73Dec 27, 2024 02:5737,24841,578,34021,604,00002,486,272
74Dec 27, 2024 02:5537,26741,126,46721,615,00002,465,792
75Dec 27, 2024 02:5337,29048,409,75020,599,0001,029,0002,359,296
76Dec 27, 2024 02:5337,51646,040,34921,759,00002,392,064
77Dec 27, 2024 02:5437,75943,628,16321,900,00002,441,216
78Dec 27, 2024 02:5537,84347,839,31119,954,0001,995,0002,445,312