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 listMar 27, 2024 19:45E SequeiraE SequeiraScore: 81,222Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 19:4580,14072,877,81846,481,00002,273,280
2Mar 27, 2024 19:4580,32167,287,85746,586,00002,285,568
3Mar 27, 2024 19:4580,80571,971,73844,873,0001,994,0002,285,568
4Mar 27, 2024 19:4580,80571,443,06446,867,00002,502,656
5Mar 27, 2024 19:4580,81773,180,75845,877,000997,0002,277,376
6Mar 27, 2024 19:4581,18475,179,31145,084,0002,003,0002,285,568
7Mar 27, 2024 19:4581,22269,617,11745,105,0002,004,0002,285,568
8Mar 27, 2024 19:4581,66670,218,85446,359,0001,007,0002,285,568
9Mar 27, 2024 19:4581,79070,434,81146,429,0001,009,0002,506,752
10Mar 27, 2024 19:4582,428119,405,80147,808,16402,252,800
11Mar 27, 2024 19:4583,328113,984,68548,330,49902,224,128
12Mar 27, 2024 19:4584,064129,422,60648,757,13802,207,744