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 listDec 22, 2025 14:51Josu San MartinJosu San MartinScore: 36,605Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 22, 2025 14:5134,32448,045,59617,918,0001,990,0002,510,848
2Dec 22, 2025 14:5135,29042,596,20120,468,00002,478,080
3Dec 22, 2025 14:5135,73345,632,80419,689,0001,036,0002,396,160
4Dec 22, 2025 14:5136,03348,211,43219,904,000995,0002,400,256
5Dec 22, 2025 14:5136,60544,195,91019,209,0002,022,0002,424,832
6Dec 22, 2025 14:5136,83148,886,52819,328,0002,034,0002,269,184
7Dec 22, 2025 14:5136,95743,499,68720,415,0001,020,0002,457,600
8Dec 22, 2025 14:5137,00044,415,00919,417,0002,043,0002,519,040
9Dec 22, 2025 14:5137,44346,205,50320,683,0001,034,0002,273,280