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 listOct 13, 2026 21:21janwwwjanwwwError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 14, 2026 21:55Andrew Epstein20.97CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 14, 2026 22:000245,971,895209,812,00026,591,0002,117,632expected "866822486 ", got "943000030 "
2Oct 14, 2026 22:000245,448,667209,381,00026,508,0002,117,632expected "884557833 ", got "943000030 "
3Oct 14, 2026 22:000248,067,729211,067,00027,393,0002,117,632expected "872781954 ", got "943000030 "
4Oct 13, 2026 21:2131,72227,360,21918,399,00002,117,632
5Oct 13, 2026 21:2131,94827,416,27318,530,00002,117,632
6Oct 13, 2026 21:2131,99327,809,37118,556,00002,117,632
7Oct 13, 2026 21:2132,21727,708,37918,686,00002,117,632
8Oct 13, 2026 21:2132,59427,874,53318,905,00002,117,632
9Oct 13, 2026 21:2132,94127,980,38619,106,00002,117,632
10Oct 13, 2026 21:2133,33628,270,57219,335,00002,117,632
11Oct 13, 2026 21:2133,97528,556,17318,610,0001,096,0002,117,632
12Oct 13, 2026 21:2141,05132,657,99919,396,0004,414,0002,117,632