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 24, 2026 08:16Dominique GarmierDominique GarmierScore: 10,552Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 24, 2026 08:1610,37220,952,5154,011,0002,005,0002,514,944
2Mar 26, 2026 13:1410,39321,563,5914,019,0002,009,0002,289,664
3Mar 24, 2026 08:1610,42823,345,1975,040,0001,008,0002,293,760
4Mar 24, 2026 08:1610,44321,362,0404,038,0002,019,0002,510,848
5Mar 24, 2026 08:1610,46625,985,1904,047,0002,023,0002,510,848
6Mar 26, 2026 13:1410,52223,296,6464,069,0002,034,0002,506,752
7Mar 24, 2026 08:1610,55224,097,5744,080,0002,040,0002,289,664
8Mar 24, 2026 08:1610,59026,069,4974,095,0002,047,0002,289,664
9Mar 26, 2026 13:1410,64521,947,4254,116,0002,058,0002,510,848
10Mar 24, 2026 08:1610,66723,227,6345,156,0001,031,0002,289,664
11Mar 24, 2026 08:1610,77925,231,5344,168,0002,084,0002,342,912
12Mar 24, 2026 08:1610,85924,784,6564,199,0002,099,0002,289,664