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 listJul 13, 2025 16:55Alexey IlyukhovAlexey IlyukhovScore: 126,033Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 13, 2025 16:55121,41795,813,98865,392,0005,030,0002,265,088
2Jul 13, 2025 16:56121,55996,814,43767,483,0003,021,0002,510,848
3Jul 13, 2025 16:56122,39593,967,07367,990,0002,999,0002,510,848
4Jul 13, 2025 16:56122,86992,352,99569,257,0002,007,0002,273,280
5Jul 13, 2025 16:56123,02894,532,21668,341,0003,015,0002,400,256
6Jul 13, 2025 16:56123,08692,262,98870,385,0001,005,0002,510,848
7Jul 13, 2025 16:56126,03399,514,17870,095,0003,004,0002,502,656
8Jul 13, 2025 16:55126,35796,480,96869,272,0004,015,0002,510,848
9Jul 13, 2025 16:56127,79595,533,03070,115,0004,006,0002,506,752
10Jul 13, 2025 16:56129,257100,717,37470,971,0003,998,0002,473,984
11Jul 13, 2025 16:56133,716104,380,51373,527,0004,028,0002,461,696
12Jul 13, 2025 16:55136,026100,618,37774,901,0003,994,0002,334,720