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 10:04Wookyung JeongWookyung JeongScore: 39,809Success
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 10:0438,78345,479,50522,494,00002,367,488
2Dec 18, 2025 10:1039,10750,877,95721,651,0001,031,0002,510,848
3Dec 18, 2025 10:0639,15353,454,58721,677,0001,032,0002,506,752
4Dec 18, 2025 10:1039,79046,327,93521,072,0002,006,0002,277,376
5Dec 18, 2025 10:0639,80946,981,11722,086,0001,003,0002,310,144
6Dec 18, 2025 10:0440,08145,885,79922,237,0001,010,0002,420,736
7Dec 18, 2025 10:1040,64146,969,71722,548,0001,024,0002,437,120
8Dec 18, 2025 10:0440,77953,104,84522,624,0001,028,0002,506,752
9Dec 18, 2025 10:0641,15250,955,65720,885,0002,983,0002,371,584