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 listJan 20, 2025 14:36Oleg KovalovOleg KovalovScore: 1,385,781Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 20, 2025 14:361,382,817823,369,880796,034,0006,000,0002,498,560
2Jan 20, 2025 14:361,384,486834,288,040798,002,0005,000,0002,347,008
3Jan 20, 2025 14:361,385,409830,061,684797,541,0005,996,0002,531,328
4Jan 20, 2025 14:361,385,598832,547,765801,648,0001,999,0002,310,144
5Jan 20, 2025 14:361,385,781832,215,195800,754,0002,999,0002,301,952
6Jan 20, 2025 14:361,386,897830,550,404801,403,0002,997,0002,351,104
7Jan 20, 2025 14:361,388,028835,550,252802,060,0002,996,0002,310,144
8Jan 20, 2025 14:361,388,267833,910,064801,195,0004,000,0002,543,616
9Jan 20, 2025 14:361,393,371834,157,526803,155,0005,000,0002,342,912