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 18, 2026 17:06DmtryDmtryScore: 142,091Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 18, 2026 17:06125,80398,867,45568,968,0003,998,0002,367,488
2Mar 18, 2026 17:06128,931104,453,57867,801,0006,979,0002,383,872
3Mar 18, 2026 17:06130,362102,649,76872,586,0003,024,0002,322,432
4Mar 18, 2026 17:06137,695109,252,71673,874,0005,989,0002,281,472
5Mar 18, 2026 17:06142,091106,801,07478,393,0004,020,0002,510,848
6Mar 18, 2026 17:06143,729115,234,12279,346,0004,017,0002,506,752
7Mar 18, 2026 17:06154,952116,525,83985,878,0003,994,0002,265,088
8Mar 18, 2026 17:06160,112121,332,77086,874,0005,991,0002,297,856
9Mar 18, 2026 17:06172,872121,519,78196,256,0004,010,0002,478,080