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 13:02@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 13:0200000Error: expected "1541945 ", got "1540843 "
2Oct 16, 2024 13:0200000Error: expected "1500823 ", got "1499249 "
3Oct 16, 2024 13:0200000Error: expected "1342999 ", got "1347146 "
4Oct 16, 2024 13:0200000Error: expected "998101 ", got "996670 "
5Oct 16, 2024 13:0200000Error: expected "1702534 ", got "1704744 "
6Oct 16, 2024 13:0200000Error: expected "822561 ", got "823756 "
7Oct 16, 2024 13:0200000Error: expected "2007614 ", got "2006781 "
8Oct 16, 2024 13:0200000Error: expected "1142229 ", got "1147837 "
9Oct 16, 2024 13:0200000Error: expected "1026597 ", got "1030662 "
10Oct 16, 2024 13:0200000Error: expected "2582643 ", got "2584761 "
11Oct 16, 2024 13:0200000Error: expected "1956849 ", got "1977599 "
12Oct 16, 2024 13:0200000Error: expected "1756074 ", got "1752816 "
13Oct 16, 2024 13:0200000Error: expected "1599581 ", got "1605526 "
14Oct 16, 2024 13:0200000Error: expected "1031174 ", got "1025416 "
15Oct 16, 2024 13:0200000Error: expected "695112 ", got "696344 "
16Oct 16, 2024 13:0200000Error: expected "1002262 ", got "1030091 "
17Oct 16, 2024 13:0200000Error: expected "1647483 ", got "1650037 "
18Oct 16, 2024 13:0200000Error: expected "995298 ", got "998378 "
19Oct 16, 2024 13:0300000Error: expected "1569133 ", got "1565843 "
20Oct 16, 2024 13:0300000Error: expected "1954131 ", got "1953586 "
21Oct 16, 2024 13:0300000Error: expected "2125354 ", got "2125315 "
22Oct 16, 2024 13:0300000Error: expected "789038 ", got "836404 "
23Oct 16, 2024 13:0300000Error: expected "2639697 ", got "2639689 "
24Oct 16, 2024 13:0300000Error: expected "2055128 ", got "2093146 "
25Oct 16, 2024 13:0300000Error: expected "1672215 ", got "1670695 "
26Oct 16, 2024 13:0300000Error: expected "1011251 ", got "1011256 "
27Oct 16, 2024 13:0300000Error: expected "3856525 ", got "3965347 "
28Oct 16, 2024 13:0300000Error: expected "2937962 ", got "2935690 "
29Oct 16, 2024 13:0300000Error: expected "2710726 ", got "2795313 "
30Oct 16, 2024 13:0300000Error: expected "806589 ", got "856420 "
31Oct 16, 2024 13:0200000Error: expected "1736956 ", got "1738038 "
32Oct 16, 2024 13:0200000Error: expected "2299218 ", got "2304826 "
33Oct 16, 2024 13:0352,75958,019,23629,580,0001,020,0002,265,088
34Oct 16, 2024 13:0352,76052,556,13927,541,0003,060,0002,469,888
35Oct 16, 2024 13:0252,82452,514,82229,617,0001,021,0002,461,696
36Oct 16, 2024 13:0352,84357,208,51629,628,0001,021,0002,465,792
37Oct 16, 2024 13:0252,92159,190,88030,694,00002,445,312
38Oct 16, 2024 13:0352,96265,803,24428,671,0002,047,0002,465,792
39Oct 16, 2024 13:0353,11256,244,45127,824,0002,981,0002,441,216
40Oct 16, 2024 13:0253,26457,537,75129,897,000996,0002,461,696
41Oct 16, 2024 13:0253,26461,376,08529,897,000996,0002,461,696
42Oct 16, 2024 13:0253,27157,235,39926,911,0003,986,0002,465,792
43Oct 16, 2024 13:0353,27953,576,80128,909,0001,993,0002,465,792
44Oct 16, 2024 13:0353,36754,278,58829,955,000998,0002,469,888
45Oct 16, 2024 13:0253,47251,896,60727,013,0004,001,0002,469,888
46Oct 16, 2024 13:0253,69055,109,12629,131,0002,009,0002,461,696
47Oct 16, 2024 13:0253,70056,367,11827,128,0004,018,0002,461,696
48Oct 16, 2024 13:0253,80255,817,42726,172,0005,033,0002,506,752
49Oct 16, 2024 13:0253,81453,554,65027,185,0004,027,0002,424,832
50Oct 16, 2024 13:0253,83857,255,50229,212,0002,014,0002,502,656
51Oct 16, 2024 13:0253,87957,731,00928,226,0003,024,0002,465,792
52Oct 16, 2024 13:0254,00952,343,86027,283,0004,042,0002,465,792
53Oct 16, 2024 13:0354,02852,924,81328,304,0003,032,0002,277,376
54Oct 16, 2024 13:0254,10059,576,06429,354,0002,024,0002,453,504
55Oct 16, 2024 13:0254,10955,457,81628,346,0003,037,0002,494,464
56Oct 16, 2024 13:0354,11261,712,35827,336,0004,049,0002,465,792
57Oct 16, 2024 13:0254,16062,393,04028,373,0003,040,0002,465,792
58Oct 16, 2024 13:0254,39160,873,51729,512,0002,035,0002,441,216
59Oct 16, 2024 13:0254,49160,188,68228,547,0003,058,0002,310,144