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 28, 2026 00:060xZohan0xZohanError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:00Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:06011,098,881896,000024,576No answer to stdout. Died or killed?
2Feb 28, 2026 00:0687,17175,392,63349,548,0001,011,0002,289,664
3Feb 28, 2026 00:0687,26474,212,42749,601,0001,012,0002,289,664
4Feb 28, 2026 00:0687,74576,894,61049,895,000997,0002,363,392
5Feb 28, 2026 00:0687,75778,551,17848,903,0001,996,0002,351,104
6Feb 28, 2026 00:0687,95283,971,19349,012,0002,000,0002,342,912
7Feb 28, 2026 00:0687,99577,788,41249,036,0002,001,0002,351,104
8Feb 28, 2026 00:0688,21780,239,75249,160,0002,006,0002,355,200
9Feb 28, 2026 00:0689,11276,170,40450,672,0001,013,0002,347,008
10Feb 28, 2026 00:0691,52275,555,75751,080,0002,003,0002,289,664