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 12:18@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 12:2100000Error: expected "646848 ", got "28422354 "
2Oct 17, 2024 12:2000000Error: expected "1445477 ", got "53631743 "
3Oct 17, 2024 12:2000000Error: expected "2535875 ", got "6270449 "
4Oct 17, 2024 12:2000000Error: expected "1102292 ", got "45868946 "
5Oct 17, 2024 12:2000000Error: expected "638571 ", got "36601938 "
6Oct 17, 2024 12:2000000Error: expected "2675648 ", got "62165613 "
7Oct 17, 2024 12:2000000Error: expected "1064294 ", got "43289760 "
8Oct 17, 2024 12:2000000Error: expected "832190 ", got "30108704 "
9Oct 17, 2024 12:2000000Error: expected "1940360 ", got "37732572 "
10Oct 17, 2024 12:2000000Error: expected "1737149 ", got "54903516 "
11Oct 17, 2024 12:2000000Error: expected "558128 ", got "27822280 "
12Oct 17, 2024 12:2100000Error: expected "1404297 ", got "11642505 "
13Oct 17, 2024 12:2100000Error: expected "1992579 ", got "761517 "
14Oct 17, 2024 12:2000000Error: expected "995438 ", got "22537820 "
15Oct 17, 2024 12:2100000Error: expected "2071237 ", got "34357581 "
16Oct 17, 2024 12:2100000Error: expected "1189027 ", got "40492820 "
17Oct 17, 2024 12:2100000Error: expected "1684361 ", got "16426291 "
18Oct 17, 2024 12:2100000Error: expected "2203274 ", got "34468286 "
19Oct 17, 2024 12:2100000Error: expected "1318384 ", got "40902404 "
20Oct 17, 2024 12:2100000Error: expected "812591 ", got "49716570 "
21Oct 17, 2024 12:2100000Error: expected "1080323 ", got "25321741 "
22Oct 17, 2024 12:2100000Error: expected "1868193 ", got "20478759 "
23Oct 17, 2024 12:2100000Error: expected "1196463 ", got "60868304 "
24Oct 17, 2024 12:2100000Error: expected "1680407 ", got "57489143 "
25Oct 17, 2024 12:2100000Error: expected "1805172 ", got "31483780 "
26Oct 17, 2024 12:2100000Error: expected "1061741 ", got "46087496 "
27Oct 17, 2024 12:1800000Error: expected "2577627 ", got "17964376 "
28Oct 17, 2024 12:1800000Error: expected "1144326 ", got "52737142 "
29Oct 17, 2024 12:1800000Error: expected "1508951 ", got "25707277 "
30Oct 17, 2024 12:1800000Error: expected "1350912 ", got "23454744 "
31Oct 17, 2024 12:1800000Error: expected "561502 ", got "10792130 "
32Oct 17, 2024 12:1800000Error: expected "2983035 ", got "17271485 "
33Oct 17, 2024 12:1800000Error: expected "1679563 ", got "10099257 "
34Oct 17, 2024 12:1800000Error: expected "1102983 ", got "42699560 "
35Oct 17, 2024 12:1800000Error: expected "684414 ", got "6627279 "
36Oct 17, 2024 12:1800000Error: expected "1760633 ", got "41853071 "
37Oct 17, 2024 12:1800000Error: expected "1633501 ", got "47873726 "
38Oct 17, 2024 12:1800000Error: expected "2568771 ", got "53271166 "
39Oct 17, 2024 12:1800000Error: expected "1102396 ", got "14688671 "
40Oct 17, 2024 12:1800000Error: expected "1324219 ", got "27476965 "
41Oct 17, 2024 12:1800000Error: expected "1598328 ", got "8370246 "
42Oct 17, 2024 12:1800000Error: expected "1276436 ", got "47257492 "
43Oct 17, 2024 12:1800000Error: expected "2158997 ", got "53111113 "
44Oct 17, 2024 12:1800000Error: expected "1843186 ", got "52137152 "
45Oct 17, 2024 12:1800000Error: expected "668381 ", got "9246051 "
46Oct 17, 2024 12:1800000Error: expected "1764045 ", got "64734572 "
47Oct 17, 2024 12:1800000Error: expected "2262882 ", got "4828576 "
48Oct 17, 2024 12:1800000Error: expected "3246627 ", got "36452109 "
49Oct 17, 2024 12:1800000Error: expected "1566800 ", got "41458796 "
50Oct 17, 2024 12:1900000Error: expected "792832 ", got "49063209 "
51Oct 17, 2024 12:1900000Error: expected "2611849 ", got "23900260 "
52Oct 17, 2024 12:1900000Error: expected "640230 ", got "30841883 "