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:41@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:4300000Error: expected "754801 ", got "754659 "
2Oct 16, 2024 13:4500000Error: expected "1899529 ", got "1900243 "
3Oct 16, 2024 13:4500000Error: expected "3117969 ", got "3121412 "
4Oct 16, 2024 13:4500000Error: expected "944855 ", got "944687 "
5Oct 16, 2024 13:4500000Error: expected "1034837 ", got "1035462 "
6Oct 16, 2024 13:4500000Error: expected "1384380 ", got "1383981 "
7Oct 16, 2024 13:4300000Error: expected "2203747 ", got "2203189 "
8Oct 16, 2024 13:4300000Error: expected "1906435 ", got "1906727 "
9Oct 16, 2024 13:4300000Error: expected "1752688 ", got "1753302 "
10Oct 16, 2024 13:4300000Error: expected "717015 ", got "718615 "
11Oct 16, 2024 13:4300000Error: expected "938760 ", got "939046 "
12Oct 16, 2024 13:4300000Error: expected "1326916 ", got "1323720 "
13Oct 16, 2024 13:4300000Error: expected "775149 ", got "776183 "
14Oct 16, 2024 13:4300000Error: expected "1408035 ", got "1425301 "
15Oct 16, 2024 13:4100000Error: expected "896654 ", got "898284 "
16Oct 16, 2024 13:4300000Error: expected "775802 ", got "784087 "
17Oct 16, 2024 13:4300000Error: expected "1460661 ", got "1460697 "
18Oct 16, 2024 13:4300000Error: expected "1631419 ", got "1625796 "
19Oct 16, 2024 13:4200000Error: expected "2227583 ", got "2227454 "
20Oct 16, 2024 13:4200000Error: expected "2667466 ", got "2880120 "
21Oct 16, 2024 13:4200000Error: expected "1137058 ", got "1133448 "
22Oct 16, 2024 13:4200000Error: expected "1361204 ", got "1361072 "
23Oct 16, 2024 13:4200000Error: expected "784576 ", got "784608 "
24Oct 16, 2024 13:4100000Error: expected "1897593 ", got "1898199 "
25Oct 16, 2024 13:4100000Error: expected "1688006 ", got "1688622 "
26Oct 16, 2024 13:4100000Error: expected "1068120 ", got "1068098 "
27Oct 16, 2024 13:4100000Error: expected "1593492 ", got "1591032 "