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 21, 2026 04:12AlionAlionScore: 96,597Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 21, 2026 04:0988,72980,127,43245,409,0006,054,0002,387,968
2Jan 21, 2026 04:0988,99076,432,69146,554,0005,060,0002,461,696
3Jan 21, 2026 04:0991,76280,474,43847,197,0006,025,0002,297,856
4Jan 21, 2026 04:0991,94177,599,62348,296,0005,030,0002,338,816
5Jan 21, 2026 04:0992,22278,654,81747,434,0006,055,0002,387,968
6Jan 21, 2026 04:1294,70079,980,22448,934,0005,992,0002,342,912
7Jan 21, 2026 04:0996,59779,094,50350,024,0006,002,0002,433,024
8Jan 21, 2026 04:1298,11986,677,89051,917,0004,992,0002,416,640
9Jan 21, 2026 04:0998,86682,592,46952,312,0005,030,0002,293,760
10Jan 21, 2026 04:09117,92992,681,16862,364,0006,035,0002,527,232
11Jan 21, 2026 04:09124,89095,221,23667,406,0005,030,0002,506,752
12Jan 21, 2026 04:12130,057103,903,66269,399,0006,034,0002,514,944