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 17, 2024 06:57matsuoka-601matsuoka-601Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 17, 2024 06:5900000Error: expected "1984315 ", got "0 "stderr
2Feb 17, 2024 07:0100000Error: expected "1035910 ", got "400747 "stderr
3Feb 17, 2024 07:0100000Error: expected "2591948 ", got "0 "stderr
4Feb 17, 2024 07:0100000Error: expected "1641656 ", got "0 "stderr
5Feb 17, 2024 07:0000000Error: expected "2374439 ", got "0 "stderr
6Feb 17, 2024 07:0000000Error: expected "1153357 ", got "0 "stderr
7Feb 17, 2024 07:0000000Error: expected "911416 ", got "825428 "stderr
8Feb 17, 2024 06:5900000Error: expected "1199340 ", got "0 "stderr
9Feb 17, 2024 06:5900000Error: expected "943019 ", got "622760 "stderr
10Feb 17, 2024 06:5900000Error: expected "1306316 ", got "0 "stderr
11Feb 17, 2024 06:5900000Error: expected "1295486 ", got "0 "stderr
12Feb 17, 2024 06:5800000Error: expected "1756974 ", got "0 "stderr
13Feb 17, 2024 06:5800000Error: expected "2880623 ", got "0 "stderr
14Feb 17, 2024 06:5800000Error: expected "1408263 ", got "0 "stderr
15Feb 17, 2024 06:5800000Error: expected "2131095 ", got "0 "stderr
16Feb 17, 2024 06:5700000Error: expected "2148970 ", got "0 "stderr
17Feb 17, 2024 07:0225,22365,061,49014,629,12002,236,416
18Feb 17, 2024 07:0225,22579,124,23314,630,57502,236,416
19Feb 17, 2024 07:0227,32375,217,79115,847,06902,236,416