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 23, 2025 23:27Code HintsCode HintsScore: 96,850Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 23, 2025 23:2994,19585,517,62353,622,0001,011,0002,424,832
2Jan 23, 2025 23:2795,90076,399,38354,611,0001,011,0002,535,424
3Jan 23, 2025 23:2996,07181,890,05454,708,0001,013,0002,535,424
4Jan 23, 2025 23:2996,44385,224,92554,939,000998,0002,273,280
5Jan 23, 2025 23:2996,56977,580,64355,010,0001,000,0002,535,424
6Jan 23, 2025 23:2996,74580,410,05154,108,0002,004,0002,416,640
7Jan 23, 2025 23:2996,85078,894,02654,167,0002,006,0002,531,328
8Jan 23, 2025 23:2997,16477,368,01755,349,0001,006,0002,424,832
9Jan 23, 2025 23:2797,21476,736,88055,378,0001,006,0002,523,136
10Jan 23, 2025 23:2798,14082,814,78854,924,0001,997,0002,535,424
11Jan 23, 2025 23:2998,16782,718,65854,940,0001,997,0002,535,424
12Jan 23, 2025 23:2999,77881,668,18755,876,0001,995,0002,273,280