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 25, 2023 02:10Kenneth MaplesKenneth MaplesScore: 95,021Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2023 02:1081,681114,864,99247,375,12602,310,144
2Dec 25, 2023 02:1081,866114,059,42147,482,36202,306,048
3Dec 25, 2023 02:1082,846113,299,38048,050,39902,228,224
4Dec 25, 2023 02:1094,61784,404,50953,881,000997,0002,514,944
5Dec 25, 2023 02:1094,85577,678,17254,016,0001,000,0002,347,008
6Dec 25, 2023 02:1094,94582,519,85853,066,0002,002,0002,342,912
7Dec 25, 2023 02:1095,02180,654,22153,108,0002,004,0002,342,912
8Dec 25, 2023 02:1095,11284,361,68353,159,0002,006,0002,514,944
9Dec 25, 2023 02:1095,74078,446,86054,520,0001,009,0002,514,944
10Dec 25, 2023 02:1095,82881,690,27355,580,00002,347,008
11Dec 25, 2023 02:1096,34081,192,97653,882,0001,995,0002,514,944
12Dec 25, 2023 02:1096,76486,353,72754,119,0002,004,0002,351,104