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 28, 2025 17:13imotw2imotw2Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:40Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:500169,084,558149,531,0009,232,0002,117,632expected "867830007 ", got "2936774029675744688 "
2Oct 5, 2026 18:500159,116,442139,684,0009,063,0002,113,536expected "871788393 ", got "591813628212141030 "
3Oct 5, 2026 18:500161,201,073142,057,0008,776,0002,113,536expected "876652488 ", got "1880444142960979510 "
4Dec 28, 2025 17:13522,190330,112,192289,876,00012,994,0002,523,136
5Dec 28, 2025 17:13523,319328,747,349296,536,0006,989,0002,519,040
6Dec 28, 2025 17:13524,148327,055,121288,006,00016,000,0002,318,336
7Dec 28, 2025 17:13524,674330,979,128292,299,00012,012,0002,273,280
8Dec 28, 2025 17:13524,790331,745,172298,371,0006,007,0002,322,432
9Dec 28, 2025 17:13524,848336,604,650295,400,0009,012,0002,502,656
10Dec 28, 2025 17:13525,319329,737,644291,656,00013,029,0002,363,392
11Dec 28, 2025 17:13526,016331,529,035300,088,0005,001,0002,351,104
12Dec 28, 2025 17:13526,029328,177,702293,094,00012,003,0002,334,720