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 listNov 23, 2024 21:34E SequeiraE SequeiraScore: 75,678Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 23, 2024 21:3474,98665,712,72842,481,0001,011,0002,498,560
2Nov 23, 2024 21:5475,09869,311,21042,545,0001,012,0002,269,184
3Nov 23, 2024 21:5375,30973,735,91942,664,0001,015,0002,285,568
4Nov 23, 2024 21:5475,33474,077,20042,678,0001,016,0002,273,280
5Nov 23, 2024 21:3475,33673,227,64342,679,0001,016,0002,285,568
6Nov 23, 2024 21:5375,46074,469,57842,750,0001,017,0002,490,368
7Nov 23, 2024 21:5375,65273,922,03242,881,000997,0002,285,568
8Nov 23, 2024 21:3475,65773,614,07142,884,000997,0002,490,368
9Nov 23, 2024 21:5475,66669,388,31042,889,000997,0002,478,080
10Nov 23, 2024 21:3475,67876,359,39242,896,000997,0002,285,568
11Nov 23, 2024 21:5375,75768,174,08741,942,0001,997,0002,269,184
12Nov 23, 2024 21:5376,05266,633,96142,105,0002,005,0002,277,376
13Nov 23, 2024 21:5376,09365,249,97742,128,0002,006,0002,498,560
14Nov 23, 2024 21:5476,17868,912,92242,175,0002,008,0002,277,376
15Nov 23, 2024 21:3476,24566,028,02142,212,0002,010,0002,277,376
16Nov 23, 2024 21:3476,65976,600,73843,452,0001,010,0002,285,568
17Nov 23, 2024 21:5476,71775,274,01243,485,0001,011,0002,478,080
18Nov 23, 2024 21:5476,72867,883,72943,491,0001,011,0002,277,376