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 16:28Wookyung JeongWookyung JeongScore: 39,010Success
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 16:2838,45044,916,82820,274,0002,027,0002,408,448
2Dec 18, 2025 16:3638,62646,686,66121,385,0001,018,0002,482,176
3Dec 18, 2025 16:3038,80748,115,33721,485,0001,023,0002,400,256
4Dec 18, 2025 16:3638,83845,281,33021,503,0001,023,0002,428,928
5Dec 18, 2025 16:2838,90747,638,81221,541,0001,025,0002,506,752
6Dec 18, 2025 16:2839,00047,659,51321,592,0001,028,0002,375,680
7Dec 18, 2025 16:3039,01050,392,82821,598,0001,028,0002,342,912
8Dec 18, 2025 16:3639,03149,335,21121,609,0001,029,0002,396,160
9Dec 18, 2025 16:2839,04053,591,54121,614,0001,029,0002,408,448
10Dec 18, 2025 16:2839,45953,902,28820,896,0001,990,0002,441,216
11Dec 18, 2025 16:2839,56443,724,02521,950,000997,0002,400,256
12Dec 18, 2025 16:3040,28656,378,26422,351,0001,015,0002,363,392