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 20, 2026 23:44DmtryDmtryScore: 46,084Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 23:4444,65357,793,11924,903,000996,0002,285,568
2Mar 20, 2026 23:4444,80352,838,92124,987,000999,0002,437,120
3Mar 20, 2026 23:4445,17453,439,72524,186,0002,015,0002,347,008
4Mar 20, 2026 23:4445,75955,907,32425,520,0001,020,0002,387,968
5Mar 20, 2026 23:4446,08456,609,55126,729,00002,392,064
6Mar 20, 2026 23:4446,37951,794,97425,904,000996,0002,457,600
7Mar 20, 2026 23:4446,52160,191,31425,983,000999,0002,437,120
8Mar 20, 2026 23:4446,73351,984,93625,098,0002,007,0002,326,528
9Mar 20, 2026 23:4446,73353,597,52925,098,0002,007,0002,490,368