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 listAug 2, 2025 12:10parastromparastromScore: 218,779Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Aug 2, 2025 12:10178,481125,597,890102,514,0001,005,0002,506,752
2Aug 2, 2025 12:10191,707136,565,516108,185,0003,005,0002,289,664
3Aug 2, 2025 12:10202,022144,197,251114,169,0003,004,0002,289,664
4Aug 2, 2025 12:10202,660142,207,868115,534,0002,009,0002,510,848
5Aug 2, 2025 12:10218,779151,462,163123,895,0002,997,0002,506,752
6Aug 2, 2025 12:10229,752156,723,549132,255,0001,001,0002,285,568
7Aug 2, 2025 12:10252,819174,750,342145,631,0001,004,0002,510,848
8Aug 2, 2025 12:10253,288172,000,544145,908,000999,0002,506,752
9Aug 2, 2025 12:10254,541180,101,214147,634,00002,506,752