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 listMar 21, 2026 02:17Dominique GarmierDominique GarmierScore: 17,721Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 16, 2026 14:4017,36624,181,0058,058,0002,014,0002,363,392
2Mar 16, 2026 14:4017,44827,242,8228,096,0002,024,0002,510,848
3Mar 22, 2026 22:4217,50331,397,7668,122,0002,030,0002,293,760
4Mar 21, 2026 02:1717,63430,865,1948,183,0002,045,0002,506,752
5Mar 21, 2026 02:0417,67230,556,1359,225,0001,025,0002,506,752
6Mar 22, 2026 22:4217,69327,518,3479,236,0001,026,0002,506,752
7Mar 22, 2026 22:4217,72126,947,3888,223,0002,055,0002,293,760
8Mar 21, 2026 02:0417,83627,721,7849,311,0001,034,0002,514,944
9Mar 21, 2026 02:1717,86232,905,0438,288,0002,072,0002,289,664
10Mar 16, 2026 14:4017,92226,103,7609,356,0001,039,0002,510,848
11Mar 21, 2026 02:1717,97126,942,8329,381,0001,042,0002,347,008
12Mar 21, 2026 02:0417,97930,792,2049,386,0001,042,0002,355,200