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 20, 2025 15:41Code HintsCode HintsScore: 135,033Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 20, 2025 15:41132,541107,505,94675,876,000998,0002,535,424
2Jan 20, 2025 15:41132,688103,430,37473,961,0002,998,0002,285,568
3Jan 20, 2025 15:41132,871101,143,24975,064,0002,001,0002,437,120
4Jan 20, 2025 15:41133,726102,478,73077,561,00002,441,216
5Jan 20, 2025 15:41135,033100,615,90376,311,0002,008,0002,535,424
6Jan 20, 2025 15:41135,10299,326,76378,359,00002,441,216
7Jan 20, 2025 15:41135,171101,053,63577,394,0001,005,0002,285,568
8Jan 20, 2025 15:41135,526103,031,00476,590,0002,015,0002,441,216
9Jan 20, 2025 15:41135,536104,454,64277,604,0001,007,0002,535,424