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 listJan 2, 2026 07:43RussellStrongRussellStrongScore: 100,324Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 2, 2026 08:0099,80584,986,51855,891,0001,996,0002,301,952
2Jan 17, 2026 04:3099,81485,966,26954,898,0002,994,0002,326,528
3Jan 17, 2026 04:30100,06783,204,27956,038,0002,001,0002,314,240
4Jan 2, 2026 08:00100,07187,029,16357,041,0001,000,0002,510,848
5Jan 2, 2026 07:43100,07288,604,24954,040,0004,002,0002,510,848
6Jan 17, 2026 04:30100,09384,011,81455,052,0003,002,0002,297,856
7Jan 17, 2026 04:30100,11681,318,30357,066,0001,001,0002,314,240
8Jan 17, 2026 04:30100,32484,108,10855,179,0003,009,0002,310,144
9Jan 17, 2026 04:30101,12485,590,26758,652,00002,527,232
10Jan 17, 2026 04:30101,15586,909,78256,647,0002,023,0002,322,432
11Jan 2, 2026 07:43101,26984,879,61257,724,0001,012,0002,265,088
12Jan 17, 2026 04:30101,30787,969,48757,745,0001,013,0002,330,624
13Jan 2, 2026 08:00101,51089,298,65154,885,0003,991,0002,514,944
14Jan 2, 2026 07:43101,78179,302,60656,032,0003,001,0002,273,280
15Jan 17, 2026 04:30101,87691,786,51256,084,0003,004,0002,322,432