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:28Wookyung JeongWookyung JeongScore: 40,224Success
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:2839,17946,516,82621,692,0001,032,0002,424,832
2Dec 18, 2025 10:2839,46049,198,80120,897,0001,990,0002,506,752
3Dec 18, 2025 10:2839,48347,804,97820,909,0001,991,0002,445,312
4Dec 18, 2025 10:2839,49749,327,61420,916,0001,992,0002,412,544
5Dec 18, 2025 10:2840,22442,038,59821,302,0002,028,0002,379,776
6Dec 18, 2025 10:2840,40052,013,07022,414,0001,018,0002,437,120
7Dec 18, 2025 10:2840,53846,442,37322,490,0001,022,0002,392,064
8Dec 18, 2025 10:2840,69745,962,38822,578,0001,026,0002,449,408
9Dec 18, 2025 10:2840,72144,408,99922,592,0001,026,0002,379,776