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 listDec 29, 2025 02:46Carl BergmanCarl BergmanScore: 147,383Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 02:46146,379112,131,11280,858,0004,042,0002,449,408
2Dec 29, 2025 02:46146,633109,499,94479,044,0006,003,0002,293,760
3Dec 29, 2025 02:46146,903113,343,40480,192,0005,012,0002,285,568
4Dec 29, 2025 02:46147,038112,252,07381,269,0004,013,0002,285,568
5Dec 29, 2025 02:46147,383110,074,80580,454,0005,028,0002,347,008
6Dec 29, 2025 02:46147,448110,592,61980,490,0005,030,0002,502,656
7Dec 29, 2025 02:46147,472111,927,41081,509,0004,025,0002,510,848
8Dec 29, 2025 02:46148,452113,523,11482,098,0004,004,0002,330,624
9Dec 29, 2025 02:46149,321115,336,04882,578,0004,028,0002,502,656