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 listDec 8, 2023 15:34Brian PiccianoBrian PiccianoError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 8, 2023 15:3400000Error: Exit with code 2: + 1002 190 + 2369 158 + 1909 101 + 2971 159 + 2414 153 + 2356 31 + 1168 153 + 1157 178 + 1355 128 + 1028 128 + 1283 79 + 1843 144 + 1247 32 + 2063 135 + 1133 112 + 1565 141 + 1234 28 + 1000 38 + 2976 23 + 1009 181 + 1643 183 + ...stderr
2Dec 8, 2023 15:3500000Error: Exit with code 2: + 1523 187 + 1820 20 + 1563 63 + 1773 139 + 1030 22 + 2693 111 + 1426 172 + 1043 157 + 1569 84 + 2618 24 + 1835 65 + 1012 132 + 2697 56 + 1494 114 + 1369 86 + 1653 98 + 2970 133 + 1070 91 + 2975 163 + 1023 163 + 2268 75 + 1952 ...stderr