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 listJan 26, 2025 14:50Code HintsCode HintsScore: 93,700Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 26, 2025 14:5192,05275,728,53652,383,0001,007,0002,531,328
2Jan 26, 2025 14:5192,41775,466,09153,602,00002,416,640
3Jan 26, 2025 14:5192,84579,619,13552,853,000997,0002,535,424
4Jan 26, 2025 14:5093,12180,177,37054,010,00002,416,640
5Jan 26, 2025 14:5193,15976,610,49252,031,0002,001,0002,420,736
6Jan 26, 2025 14:5093,30580,361,31052,113,0002,004,0002,420,736
7Jan 26, 2025 14:5093,70079,270,03454,346,00002,531,328
8Jan 26, 2025 14:5093,87676,869,17454,448,00002,420,736
9Jan 26, 2025 14:5193,91279,581,33054,469,00002,273,280
10Jan 26, 2025 14:5194,66079,138,06453,905,000998,0002,416,640
11Jan 26, 2025 14:5095,21777,750,04153,218,0002,008,0002,531,328
12Jan 26, 2025 14:5095,51088,606,34654,389,0001,007,0002,535,424