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 listJul 15, 2026 20:59Andrew TsibinAndrew TsibinError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:59Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 15:29011,122,5341,139,000024,576No answer to stdout. Died or killed?stderr
2Jul 15, 2026 21:0037,52231,979,04320,735,0001,028,0002,113,536
3Jul 15, 2026 21:0037,97931,671,60121,006,0001,022,0002,113,536
4Jul 15, 2026 21:0038,40031,808,98821,270,0001,002,0002,113,536
5Jul 15, 2026 21:0038,61532,515,35421,365,0001,032,0002,113,536
6Jul 15, 2026 21:0038,84332,778,61721,508,0001,021,0002,113,536
7Jul 15, 2026 21:0038,86732,597,19121,525,0001,018,0002,113,536
8Jul 15, 2026 21:0038,98632,274,41421,580,0001,032,0002,113,536
9Jul 15, 2026 21:0039,19432,314,56621,703,0001,030,0002,113,536
10Jul 15, 2026 21:0039,25132,408,45222,766,00002,113,536