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 16, 2025 05:01HattonuriHattonuriScore: 42,505Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 16, 2025 05:0239,51648,580,50420,926,0001,993,0002,523,136
2Dec 16, 2025 05:0139,80948,781,53221,082,0002,007,0002,510,848
3Dec 16, 2025 05:0239,88454,281,23022,128,0001,005,0002,531,328
4Dec 16, 2025 05:0139,99144,259,07922,187,0001,008,0002,527,232
5Dec 16, 2025 05:0240,81048,808,64622,641,0001,029,0002,469,888
6Dec 16, 2025 05:0241,25048,675,72121,932,0001,993,0002,527,232
7Dec 16, 2025 05:0142,42849,421,58024,608,00002,531,328
8Dec 16, 2025 05:0142,50555,311,29923,626,0001,027,0002,269,184
9Dec 16, 2025 05:0142,51948,907,02823,634,0001,027,0002,531,328
10Dec 16, 2025 05:0142,66946,632,28224,748,00002,531,328
11Dec 16, 2025 05:0142,71453,185,49324,774,00002,510,848
12Dec 16, 2025 05:0142,94348,878,97523,911,000996,0002,531,328
13Dec 16, 2025 05:0242,94353,002,80323,911,000996,0002,519,040
14Dec 16, 2025 05:0243,20747,807,33623,056,0002,004,0002,523,136
15Dec 16, 2025 05:0143,82651,626,03925,419,00002,342,912