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 listJul 3, 2024 15:13E SequeiraE SequeiraScore: 77,847Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 3, 2024 15:1377,65766,846,36044,041,0001,000,0002,269,184
2Jul 3, 2024 15:1377,66968,490,58443,046,0002,002,0002,510,848
3Jul 3, 2024 15:1377,67971,751,43743,052,0002,002,0002,285,568
4Jul 3, 2024 15:1377,68172,113,43443,053,0002,002,0002,285,568
5Jul 3, 2024 15:1377,72968,603,15444,082,0001,001,0002,265,088
6Jul 3, 2024 15:1377,79768,120,68643,117,0002,005,0002,260,992
7Jul 3, 2024 15:1377,84771,297,08543,145,0002,006,0002,285,568
8Jul 3, 2024 15:1378,60569,668,68644,578,0001,013,0002,519,040
9Jul 3, 2024 15:1378,69076,462,32843,656,0001,984,0002,519,040
10Jul 3, 2024 15:1381,299125,510,67247,153,51902,236,416
11Jul 3, 2024 15:1382,213113,660,47147,683,71602,236,416
12Jul 3, 2024 15:1383,211106,561,08948,262,35802,232,320