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 18, 2025 11:46AnSaAnSaScore: 2,369,198Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 18, 2025 11:442,299,0141,357,987,6301,327,431,0005,997,0002,277,376
2Jan 18, 2025 11:442,319,1721,368,526,7381,341,123,0003,997,0002,469,888
3Jan 18, 2025 11:462,324,0211,375,466,0931,345,933,0001,999,0002,277,376
4Jan 18, 2025 11:272,343,9641,382,995,8781,357,500,0001,999,0002,285,568
5Jan 18, 2025 11:442,369,1981,399,217,5671,370,138,0003,997,0002,273,280
6Jan 18, 2025 11:272,392,5331,414,718,2361,385,670,0001,999,0002,285,568
7Jan 18, 2025 11:272,393,4481,416,548,3311,386,202,0001,998,0002,277,376
8Jan 18, 2025 11:462,442,5951,438,455,5031,414,706,0001,999,0002,277,376
9Jan 18, 2025 11:462,602,9741,535,865,7321,505,726,0003,999,0002,473,984