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 14:15@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 14:1500000Error: expected "1350617 ", got "1346356 "
2Oct 16, 2024 14:1500000Error: expected "2517797 ", got "2676909 "
3Oct 16, 2024 14:1500000Error: expected "1685825 ", got "1972318 "
4Oct 16, 2024 14:1500000Error: expected "2146669 ", got "2146934 "
5Oct 16, 2024 14:1500000Error: expected "1113526 ", got "1143243 "
6Oct 16, 2024 14:1500000Error: expected "2902682 ", got "3154493 "
7Oct 16, 2024 14:1500000Error: expected "1469768 ", got "1470566 "
8Oct 16, 2024 14:1500000Error: expected "2075752 ", got "2075262 "
9Oct 16, 2024 14:1500000Error: expected "802840 ", got "819025 "
10Oct 16, 2024 14:1500000Error: expected "1683876 ", got "1682653 "
11Oct 16, 2024 14:1500000Error: expected "979665 ", got "980049 "
12Oct 16, 2024 14:1500000Error: expected "1167218 ", got "1168871 "
13Oct 16, 2024 14:1500000Error: expected "1468694 ", got "1468494 "
14Oct 16, 2024 14:1500000Error: expected "1892502 ", got "1915644 "
15Oct 16, 2024 14:1500000Error: expected "1494258 ", got "1932097 "
16Oct 16, 2024 14:1500000Error: expected "879741 ", got "883587 "
17Oct 16, 2024 14:1500000Error: expected "1610359 ", got "1614358 "
18Oct 16, 2024 14:1500000Error: expected "765762 ", got "764145 "
19Oct 17, 2024 09:0400000Error: expected "1394986 ", got "1396018 "
20Oct 17, 2024 09:0400000Error: expected "965259 ", got "1000357 "
21Oct 17, 2024 09:0400000Error: expected "2012834 ", got "2029232 "
22Oct 17, 2024 09:0400000Error: expected "1915872 ", got "2195991 "
23Oct 17, 2024 09:0400000Error: expected "994228 ", got "996762 "
24Oct 16, 2024 14:1546,58652,190,55225,019,0002,001,0002,469,888
25Oct 16, 2024 14:1546,65050,395,36423,049,0004,008,0002,494,464
26Oct 16, 2024 14:1546,72451,337,45125,093,0002,007,0002,465,792
27Oct 16, 2024 14:1546,76949,444,37123,108,0004,018,0002,469,888
28Oct 16, 2024 14:1546,81255,677,58723,129,0004,022,0002,469,888
29Oct 16, 2024 14:1546,88154,358,65024,170,0003,021,0002,453,504
30Oct 16, 2024 14:1546,88858,224,59324,174,0003,021,0002,453,504
31Oct 16, 2024 14:1546,90553,262,36323,175,0004,030,0002,416,640
32Oct 16, 2024 14:1546,93349,479,89226,213,0001,008,0002,473,984
33Oct 16, 2024 14:1546,93848,457,44323,191,0004,033,0002,465,792
34Oct 16, 2024 14:1546,94350,320,64623,194,0004,033,0002,498,560
35Oct 16, 2024 14:1546,99554,739,33524,229,0003,028,0002,363,392
36Oct 16, 2024 14:1547,01050,990,86724,237,0003,029,0002,473,984
37Oct 16, 2024 14:1547,03154,848,74725,258,0002,020,0002,289,664
38Oct 16, 2024 14:1547,07957,032,35725,284,0002,022,0002,473,984
39Oct 16, 2024 14:1547,15048,461,41526,335,0001,012,0002,314,240
40Oct 16, 2024 14:1547,24055,654,02624,355,0003,044,0002,461,696
41Oct 16, 2024 14:1547,32850,393,75926,434,0001,016,0002,445,312
42Oct 16, 2024 14:1547,34553,423,47622,375,0005,085,0002,289,664
43Oct 16, 2024 14:1547,72453,725,61124,605,0003,075,0002,334,720
44Oct 17, 2024 09:0448,67257,534,62126,214,0002,016,0002,289,664
45Oct 17, 2024 09:0448,71754,800,66826,238,0002,018,0002,265,088
46Oct 17, 2024 09:0448,75355,464,64025,248,0003,029,0002,297,856
47Oct 17, 2024 09:0548,89353,203,11926,333,0002,025,0002,465,792
48Oct 17, 2024 09:0548,92252,890,56923,308,0005,067,0002,289,664
49Oct 17, 2024 09:0549,01952,207,24823,354,0005,077,0002,461,696
50Oct 16, 2024 14:1550,17151,339,76526,089,0003,010,0002,461,696