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 listFeb 19, 2026 01:38Josu San MartinJosu San MartinScore: 3,752Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 19, 2026 01:393,47217,404,45202,014,0002,306,048
2Feb 19, 2026 01:373,60921,349,80302,093,0002,318,336
3Feb 19, 2026 01:353,62918,040,37302,105,0002,400,256
4Feb 19, 2026 01:353,68417,619,92302,137,0002,514,944
5Feb 19, 2026 01:263,71422,883,53402,154,0002,478,080
6Feb 19, 2026 01:373,74822,266,7731,087,0001,087,0002,297,856
7Feb 19, 2026 01:383,74820,404,8131,087,0001,087,0002,445,312
8Feb 19, 2026 01:263,75219,227,1791,088,0001,088,0002,473,984
9Feb 19, 2026 01:393,77122,211,56002,187,0002,461,696
10Feb 19, 2026 01:383,77217,534,0401,094,0001,094,0002,502,656
11Feb 19, 2026 01:393,78622,727,5231,098,0001,098,0002,465,792
12Feb 19, 2026 01:373,81716,537,3101,107,0001,107,0002,285,568
13Feb 19, 2026 01:263,84720,469,27802,231,0002,437,120
14Feb 19, 2026 01:353,88321,609,8091,126,0001,126,0002,437,120
15Feb 19, 2026 01:384,02419,799,1541,167,0001,167,0002,355,200