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:30@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:3100000Error: expected "1462822 ", got "1486485 "
2Oct 17, 2024 13:3000000Error: expected "875177 ", got "876362 "
3Oct 17, 2024 13:3000000Error: expected "680177 ", got "680649 "
4Oct 17, 2024 13:3000000Error: expected "1393174 ", got "1393591 "
5Oct 17, 2024 13:3000000Error: expected "2392612 ", got "2468188 "
6Oct 17, 2024 13:3000000Error: expected "1156275 ", got "1159215 "
7Oct 17, 2024 13:3000000Error: expected "2083328 ", got "2083648 "
8Oct 17, 2024 13:3000000Error: expected "2104524 ", got "2107288 "
9Oct 17, 2024 13:3000000Error: expected "669045 ", got "670131 "
10Oct 17, 2024 13:3000000Error: expected "1415949 ", got "1421186 "
11Oct 17, 2024 13:3000000Error: expected "1395296 ", got "1395107 "
12Oct 17, 2024 13:3000000Error: expected "769666 ", got "800386 "
13Oct 17, 2024 13:3100000Error: expected "1297172 ", got "1296269 "
14Oct 17, 2024 13:3000000Error: expected "1610630 ", got "1610734 "
15Oct 17, 2024 13:3100000Error: expected "1779552 ", got "1780998 "
16Oct 17, 2024 13:3100000Error: expected "2247149 ", got "2251398 "
17Oct 17, 2024 13:3100000Error: expected "1108021 ", got "1150924 "
18Oct 17, 2024 13:3100000Error: expected "1143699 ", got "1142492 "
19Oct 17, 2024 13:3100000Error: expected "1101736 ", got "1103113 "
20Oct 17, 2024 13:3100000Error: expected "1825903 ", got "1825712 "
21Oct 17, 2024 13:3100000Error: expected "801754 ", got "800064 "
22Oct 17, 2024 13:3100000Error: expected "941752 ", got "942236 "
23Oct 17, 2024 13:3100000Error: expected "1283691 ", got "1297571 "
24Oct 17, 2024 13:3100000Error: expected "1520111 ", got "1519994 "
25Oct 17, 2024 13:3100000Error: expected "1013443 ", got "1013498 "
26Oct 17, 2024 13:3000000Error: expected "569224 ", got "568630 "
27Oct 17, 2024 13:3000000Error: expected "1062850 ", got "1065500 "
28Oct 17, 2024 13:3000000Error: expected "2250616 ", got "2253943 "
29Oct 17, 2024 13:3000000Error: expected "2508982 ", got "2511825 "
30Oct 17, 2024 13:3000000Error: expected "1011362 ", got "1010771 "
31Oct 17, 2024 13:3000000Error: expected "2394613 ", got "2465274 "
32Oct 17, 2024 13:3022,50332,746,7539,036,0004,016,0002,314,240
33Oct 17, 2024 13:3122,51028,619,20910,043,0003,013,0002,473,984
34Oct 17, 2024 13:3122,51636,967,61411,050,0002,009,0002,457,600
35Oct 17, 2024 13:3022,58333,090,4899,068,0004,030,0002,473,984
36Oct 17, 2024 13:3122,61633,084,0839,081,0004,036,0002,465,792
37Oct 17, 2024 13:3122,71435,821,26210,134,0003,040,0002,306,048
38Oct 17, 2024 13:3122,76030,625,55410,155,0003,046,0002,514,944
39Oct 17, 2024 13:3022,76634,890,7898,126,0005,078,0002,461,696
40Oct 17, 2024 13:3022,82629,293,17410,184,0003,055,0002,469,888
41Oct 17, 2024 13:3122,83130,502,8829,168,0004,074,0002,285,568
42Oct 17, 2024 13:3122,88636,153,7088,169,0005,105,0002,473,984
43Oct 17, 2024 13:3022,91630,185,0439,202,0004,089,0002,461,696
44Oct 17, 2024 13:3022,93834,407,77610,234,0003,070,0002,465,792
45Oct 17, 2024 13:3022,98133,688,26811,279,0002,050,0002,465,792
46Oct 17, 2024 13:3023,04328,778,8778,225,0005,140,0002,469,888
47Oct 17, 2024 13:3123,25038,437,9949,336,0004,149,0002,469,888
48Oct 17, 2024 13:3124,39136,306,02912,126,0002,021,0002,408,448
49Oct 17, 2024 13:3024,49539,011,04111,163,0003,044,0002,465,792