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 21, 2026 00:55olliecrowolliecrowScore: 94,532Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 21, 2026 00:5588,96580,506,23350,589,0001,011,0002,449,408stderr
2Apr 21, 2026 00:5591,91566,666,18852,306,0001,005,0002,351,104stderr
3Apr 21, 2026 00:5592,33275,771,14652,543,0001,010,0002,396,160stderr
4Apr 21, 2026 00:5593,62579,749,80853,298,0001,005,0002,408,448stderr
5Apr 21, 2026 00:5594,53279,621,14052,836,0001,993,0002,396,160stderr
6Apr 21, 2026 00:5594,60579,666,75652,876,0001,995,0002,351,104stderr
7Apr 21, 2026 00:5597,19680,629,15655,368,0001,006,0002,334,720stderr
8Apr 21, 2026 00:5598,27276,641,36854,999,0001,999,0002,260,992stderr
9Apr 21, 2026 00:5598,86771,608,80755,331,0002,012,0002,387,968stderr