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 26, 2026 14:30Dominique GarmierDominique GarmierError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 18:40Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 18:40011,318,4042,043,000020,480No answer to stdout. Died or killed?
2Mar 26, 2026 15:367,63622,548,9793,322,0001,107,0002,486,272
3Mar 26, 2026 15:367,75224,201,7233,372,0001,124,0002,310,144
4Mar 26, 2026 14:037,75321,502,7013,373,0001,124,0002,486,272
5Mar 26, 2026 14:037,80022,996,0423,393,0001,131,0002,482,176
6Mar 26, 2026 14:037,80323,850,1141,131,0003,395,0002,277,376
7Mar 26, 2026 15:107,82122,374,6242,268,0002,268,0002,326,528
8Mar 26, 2026 14:037,82822,289,4042,270,0002,270,0002,482,176
9Mar 26, 2026 15:367,83821,441,7272,273,0002,273,0002,281,472
10Mar 26, 2026 15:367,85723,584,7854,557,00002,342,912
11Mar 26, 2026 14:037,85721,797,2693,418,0001,139,0002,318,336
12Mar 26, 2026 14:307,90523,698,0133,439,0001,146,0002,330,624
13Mar 26, 2026 15:367,90923,942,6864,587,00002,334,720
14Mar 26, 2026 15:367,91221,472,0253,442,0001,147,0002,318,336
15Mar 26, 2026 15:107,91923,539,0723,445,0001,148,0002,314,240
16Mar 26, 2026 15:367,92222,443,7694,595,00002,318,336
17Mar 26, 2026 15:367,94019,933,3373,454,0001,151,0002,322,432
18Mar 26, 2026 15:367,98322,000,5353,473,0001,157,0002,326,528
19Mar 26, 2026 15:368,00020,433,8004,640,00002,342,912
20Mar 26, 2026 15:368,02123,224,8463,489,0001,163,0002,269,184
21Mar 26, 2026 15:368,03623,259,1163,496,0001,165,0002,478,080
22Mar 26, 2026 14:038,05222,924,6742,335,0002,335,0002,314,240
23Mar 26, 2026 14:038,06927,169,1152,340,0002,340,0002,330,624
24Mar 26, 2026 14:308,08425,507,1533,517,0001,172,0002,318,336
25Mar 26, 2026 14:038,13121,041,2843,537,0001,179,0002,326,528
26Mar 26, 2026 14:308,16626,873,7503,552,0001,184,0002,273,280
27Mar 26, 2026 15:108,19019,903,2133,563,0001,187,0002,269,184
28Mar 26, 2026 14:038,22426,760,3382,385,0002,385,0002,482,176