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 18, 2025 11:24Wookyung JeongWookyung JeongScore: 40,457Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 18, 2025 11:2438,71044,983,64021,432,0001,020,0002,285,568
2Dec 18, 2025 11:2439,48453,096,21521,906,000995,0002,269,184
3Dec 18, 2025 11:2439,55944,397,64520,949,0001,995,0002,396,160
4Dec 18, 2025 11:2439,99546,085,06921,180,0002,017,0002,355,200
5Dec 18, 2025 11:2440,14350,524,36822,271,0001,012,0002,428,928
6Dec 18, 2025 11:2440,37452,164,96922,399,0001,018,0002,297,856
7Dec 18, 2025 11:2440,45744,421,97022,445,0001,020,0002,490,368
8Dec 18, 2025 11:2440,47646,012,45822,456,0001,020,0002,482,176
9Dec 18, 2025 11:2440,48450,614,52222,461,0001,020,0002,490,368
10Dec 18, 2025 11:2440,56748,969,73922,506,0001,023,0002,326,528
11Dec 18, 2025 11:2440,79148,590,57223,659,00002,375,680
12Dec 18, 2025 11:2440,90548,906,64523,725,00002,375,680