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 listMar 2, 2026 21:41JuneLJuneLScore: 70,703Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 2, 2026 21:4161,82462,693,10528,886,0006,972,0002,269,184
2Mar 2, 2026 21:4162,65562,898,99531,293,0005,047,0002,400,256
3Mar 2, 2026 21:4063,61268,467,69729,915,0006,980,0002,379,776
4Mar 2, 2026 21:4063,69362,987,12831,950,0004,992,0002,310,144
5Mar 2, 2026 21:4064,36964,515,18333,298,0004,036,0002,306,048
6Mar 2, 2026 21:4064,92162,072,41133,584,0004,070,0002,351,104
7Mar 2, 2026 21:4070,50967,881,63734,911,0005,984,0002,478,080
8Mar 2, 2026 21:4070,70367,114,76336,007,0005,001,0002,318,336
9Mar 2, 2026 21:4170,77869,168,38735,044,0006,007,0002,433,024
10Mar 2, 2026 21:4170,99766,828,30135,152,0006,026,0002,367,488
11Mar 2, 2026 21:4072,15365,397,20835,871,0005,978,0002,441,216
12Mar 2, 2026 21:4073,20068,194,48837,402,0005,054,0002,408,448
13Mar 2, 2026 21:4173,90070,288,93136,882,0005,980,0002,404,352
14Mar 2, 2026 21:4073,90573,659,42836,884,0005,981,0002,408,448
15Mar 2, 2026 21:4174,05974,743,37036,961,0005,993,0002,441,216