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 11:12Wookyung JeongWookyung JeongScore: 40,555Success
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 11:1238,51745,269,85821,325,0001,015,0002,523,136
2Dec 18, 2025 11:1339,20351,527,47021,705,0001,033,0002,424,832
3Dec 18, 2025 11:1239,48349,207,63920,909,0001,991,0002,420,736
4Dec 18, 2025 11:1239,82245,268,19022,093,0001,004,0002,400,256
5Dec 18, 2025 11:1340,07437,328,10722,233,0001,010,0002,490,368
6Dec 18, 2025 11:1240,38644,718,06822,406,0001,018,0002,310,144
7Dec 18, 2025 11:1240,54847,102,81822,496,0001,022,0002,478,080
8Dec 18, 2025 11:1240,55550,224,85522,500,0001,022,0002,490,368
9Dec 18, 2025 11:1240,57451,779,40322,510,0001,023,0002,371,584
10Dec 18, 2025 11:1240,60249,431,84322,526,0001,023,0002,318,336
11Dec 18, 2025 11:1240,62647,728,04122,539,0001,024,0002,396,160
12Dec 18, 2025 11:1240,64745,610,34322,550,0001,025,0002,412,544
13Dec 18, 2025 11:1340,79149,777,99722,631,0001,028,0002,478,080
14Dec 18, 2025 11:1240,86449,294,77823,701,00002,392,064
15Dec 18, 2025 11:1240,96451,588,57322,726,0001,033,0002,441,216