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 listFeb 16, 2024 06:37Tejas G.Tejas G.Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 16, 2024 06:3800000Error: expected "1660687 ", got "1279191 "
2Feb 16, 2024 06:3800000Error: expected "846781 ", got "792051 "
3Feb 16, 2024 06:3800000Error: expected "1242378 ", got "1028126 "
4Feb 16, 2024 06:3800000Error: expected "1011774 ", got "953668 "
5Feb 16, 2024 06:3800000Error: expected "647361 ", got "666137 "
6Feb 16, 2024 06:3800000Error: expected "3130690 ", got "2093365 "
7Feb 16, 2024 06:3800000Error: expected "1648188 ", got "549493 "
8Feb 16, 2024 06:3800000Error: expected "1661746 ", got "283278 "
9Feb 16, 2024 06:3800000Error: expected "970760 ", got "992586 "
10Feb 16, 2024 06:3800000Error: expected "1127248 ", got "818234 "
11Feb 16, 2024 06:3800000Error: expected "936859 ", got "935849 "
12Feb 16, 2024 06:3800000Error: expected "1294594 ", got "870403 "
13Feb 16, 2024 06:3800000Error: expected "2289512 ", got "1830915 "
14Feb 16, 2024 06:3900000Error: expected "2007795 ", got "1071689 "
15Feb 16, 2024 06:3900000Error: expected "1792958 ", got "1092518 "
16Feb 16, 2024 06:3900000Error: expected "1197846 ", got "1228135 "
17Feb 16, 2024 07:3000000Error: expected "584155 ", got "767765 "
18Feb 16, 2024 06:3700000Error: expected "1927107 ", got "0 "
19Feb 16, 2024 06:3800000Error: expected "2529175 ", got "2544567 "
20Feb 16, 2024 06:3800000Error: expected "750903 ", got "684562 "
21Feb 16, 2024 06:3800000Error: expected "1657758 ", got "1510096 "
22Feb 16, 2024 06:3800000Error: expected "630975 ", got "553392 "
23Feb 16, 2024 06:3800000Error: expected "980991 ", got "231616 "
24Feb 16, 2024 06:3800000Error: expected "854956 ", got "730396 "
25Feb 16, 2024 06:3700000Error: expected "1103895 ", got "971545 "
26Feb 16, 2024 06:3700000Error: expected "2062273 ", got "0 "
27Feb 16, 2024 06:3700000Error: expected "1476799 ", got "1420213 "
28Feb 16, 2024 06:3700000Error: expected "1711277 ", got "1210089 "
29Feb 16, 2024 06:3700000Error: expected "2306852 ", got "1557487 "
30Feb 16, 2024 06:3700000Error: expected "1634840 ", got "1827881 "
31Feb 16, 2024 06:3700000Error: expected "2015940 ", got "2572758 "
32Feb 16, 2024 06:3700000Error: expected "1983750 ", got "1128150 "
33Feb 16, 2024 06:3700000Error: expected "743385 ", got "590709 "