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 listJan 30, 2026 14:25olliecrowolliecrowError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:02Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:04021,897,5374,539,0007,136,0002,121,728expected "866816654 ", got "1 "
2Sep 11, 2026 14:04022,608,1404,483,0007,154,0002,121,728expected "876688825 ", got "1 "
3Sep 11, 2026 14:04022,822,6074,533,0007,459,0002,121,728expected "868814749 ", got "1 "
4Jan 30, 2026 14:25117,07892,823,00562,838,0005,067,0002,441,216
5Jan 30, 2026 14:25117,95996,850,46863,386,0005,030,0002,400,256
6Jan 30, 2026 14:25120,86998,395,96961,091,0009,013,0002,400,256
7Jan 30, 2026 14:25121,46997,075,09163,407,0007,045,0002,322,432
8Jan 30, 2026 14:25124,593100,600,16464,235,0008,029,0002,465,792
9Jan 30, 2026 14:25124,87499,720,56066,392,0006,035,0002,277,376
10Jan 30, 2026 14:25125,086103,309,49165,497,0007,053,0002,367,488
11Jan 30, 2026 14:25126,576101,145,20867,380,0006,034,0002,494,464
12Jan 30, 2026 14:25129,145102,919,06368,912,0005,992,0002,494,464