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 15, 2026 19:12richardbranway1richardbranway1Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 15:01Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 15:55018,506,6276,442,0001,169,0002,121,728expected "876691373 ", got "6 "
2Sep 11, 2026 15:55018,475,7356,420,0001,182,0002,121,728expected "870735736 ", got "0 "
3Sep 11, 2026 15:55018,706,4286,840,0001,037,0002,121,728expected "871746160 ", got "0 "
4Mar 15, 2026 19:128,11219,949,4083,529,0001,176,0002,510,848
5Mar 15, 2026 19:288,12421,416,5023,534,0001,178,0002,277,376
6Mar 15, 2026 19:108,14021,569,4464,721,00002,510,848
7Mar 15, 2026 19:108,14025,371,8463,541,0001,180,0002,510,848
8Mar 15, 2026 19:108,14721,525,3674,725,00002,510,848
9Mar 15, 2026 19:108,14822,490,1904,726,00002,510,848
10Mar 15, 2026 19:108,20023,259,0213,567,0001,189,0002,281,472
11Mar 15, 2026 19:288,20720,477,7383,570,0001,190,0002,506,752
12Mar 15, 2026 19:108,21721,079,0963,575,0001,191,0002,506,752
13Mar 15, 2026 19:108,30021,922,3484,814,00002,281,472
14Mar 15, 2026 19:128,43123,909,1012,934,0001,956,0002,273,280
15Mar 15, 2026 19:108,47820,757,9252,950,0001,967,0002,277,376
16Mar 15, 2026 19:128,49720,380,2513,696,0001,232,0002,510,848
17Mar 15, 2026 19:288,49727,409,8142,957,0001,971,0002,281,472
18Mar 15, 2026 19:108,51023,660,7382,962,0001,974,0002,510,848