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 listApr 26, 2026 23:00Victor MerckléVictor MerckléScore: 683,222Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 26, 2026 23:00675,112405,576,609373,585,00017,980,0002,519,040stderr
2Apr 26, 2026 23:00679,643414,749,968385,189,0009,004,0002,306,048stderr
3Apr 26, 2026 23:00679,720411,838,968385,233,0009,005,0002,560,000stderr
4Apr 26, 2026 23:00681,150412,692,238382,032,00013,035,0002,347,008stderr
5Apr 26, 2026 23:00683,222411,289,618380,259,00016,010,0002,506,752stderr
6Apr 26, 2026 23:00683,487412,269,835379,405,00017,018,0002,326,528stderr
7Apr 26, 2026 23:00683,820414,235,290381,631,00014,985,0002,301,952stderr
8Apr 26, 2026 23:00684,046416,968,459379,758,00016,989,0002,551,808stderr
9Apr 26, 2026 23:00686,382413,465,885377,097,00021,005,0002,564,096stderr