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 29, 2025 23:02Code HintsCode HintsScore: 110,959Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 29, 2025 23:02110,13389,931,19362,879,000998,0002,420,736
2Mar 29, 2025 23:03110,14886,539,32261,890,0001,996,0002,531,328
3Mar 29, 2025 23:03110,48386,957,02362,078,0002,002,0002,527,232
4Mar 29, 2025 23:03110,49187,872,43363,084,0001,001,0002,269,184
5Mar 29, 2025 23:02110,54887,290,62362,115,0002,003,0002,428,928
6Mar 29, 2025 23:03110,63691,382,05263,167,0001,002,0002,424,832
7Mar 29, 2025 23:03110,80790,060,23564,268,00002,420,736
8Mar 29, 2025 23:02110,91086,462,45563,323,0001,005,0002,531,328
9Mar 29, 2025 23:02110,94784,861,76664,349,00002,420,736
10Mar 29, 2025 23:02110,95992,002,16764,356,00002,531,328
11Mar 29, 2025 23:02110,96090,358,58064,357,00002,424,832
12Mar 29, 2025 23:03111,01990,342,88463,385,0001,006,0002,416,640
13Mar 29, 2025 23:03111,14386,102,01463,456,0001,007,0002,527,232
14Mar 29, 2025 23:02111,15586,951,07064,470,00002,531,328
15Mar 29, 2025 23:02111,21089,764,92163,495,0001,007,0002,527,232
16Mar 29, 2025 23:03111,43188,148,17764,630,00002,523,136
17Mar 29, 2025 23:03111,44093,752,27464,635,00002,527,232
18Mar 29, 2025 23:02111,87191,084,00362,889,0001,996,0002,531,328