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 18, 2026 22:38Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 14:00Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 14:00010,764,5721,639,00002,117,632expected "999999000 ", got "0 "
2Jul 5, 2026 14:0009,476,6091,633,00002,117,632expected "999999000 ", got "0 "
3Jul 5, 2026 14:00011,055,3701,641,00002,117,632expected "999999000 ", got "0 "
4Mar 18, 2026 22:393,40020,126,929986,000986,0002,539,520
5Mar 18, 2026 22:403,41016,973,100989,000989,0002,523,136
6Mar 18, 2026 22:403,52822,258,6141,023,0001,023,0002,342,912
7Mar 18, 2026 22:393,54822,579,00002,058,0002,338,816
8Mar 18, 2026 22:403,63121,624,3871,053,0001,053,0002,539,520
9Mar 18, 2026 22:393,64123,684,5961,056,0001,056,0002,322,432
10Mar 18, 2026 22:403,64516,655,1221,057,0001,057,0002,400,256
11Mar 18, 2026 22:403,68319,224,6131,068,0001,068,0002,412,544
12Mar 18, 2026 22:393,72823,611,7431,081,0001,081,0002,285,568
13Mar 18, 2026 22:393,74818,156,4831,087,0001,087,0002,396,160
14Mar 18, 2026 22:403,77817,703,97502,191,0002,371,584
15Mar 18, 2026 22:394,12822,149,0871,197,0001,197,0002,342,912
16Mar 18, 2026 22:385,35018,239,8182,069,0001,034,0002,371,584
17Mar 18, 2026 22:385,42820,499,8422,099,0001,049,0002,355,200
18Mar 18, 2026 22:385,47918,347,0212,119,0001,059,0002,371,584