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 1, 2025 12:55Code HintsCode HintsScore: 93,812Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 1, 2025 12:5691,98378,230,56852,344,0001,006,0002,420,736
2Feb 1, 2025 12:5692,60984,072,64752,700,0001,013,0002,535,424
3Feb 1, 2025 12:5792,89881,259,34350,888,0002,993,0002,535,424
4Feb 1, 2025 12:5692,90577,827,02452,888,000997,0002,416,640
5Feb 1, 2025 12:5693,04182,940,81351,966,0001,998,0002,273,280
6Feb 1, 2025 12:5693,39082,014,70853,163,0001,003,0002,412,544
7Feb 1, 2025 12:5593,59384,630,22053,279,0001,005,0002,535,424
8Feb 1, 2025 12:5593,68481,539,79553,331,0001,006,0002,535,424
9Feb 1, 2025 12:5593,76778,276,11053,378,0001,007,0002,273,280
10Feb 1, 2025 12:5693,81283,827,74553,404,0001,007,0002,535,424
11Feb 1, 2025 12:5694,03177,746,80853,529,0001,009,0002,416,640
12Feb 1, 2025 12:5794,11078,301,60053,574,0001,010,0002,535,424
13Feb 1, 2025 12:5594,11278,699,58654,585,00002,535,424
14Feb 1, 2025 12:5694,29180,377,32254,689,00002,535,424
15Feb 1, 2025 12:5694,36785,977,37653,738,000995,0002,412,544
16Feb 1, 2025 12:5594,57678,793,49453,857,000997,0002,273,280
17Feb 1, 2025 12:5594,63384,109,54654,887,00002,273,280
18Feb 1, 2025 12:5794,65579,084,65952,904,0001,996,0002,416,640