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 listSep 7, 2025 20:34Daniel LubarovDaniel LubarovError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 19:00Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:06011,390,5271,203,000032,768Exit with code 101: thread 'main' panicked at main.rs:24:9: index out of bounds: the len is 4096 but the index is 21980 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Sep 7, 2025 20:34654,148410,307,570377,410,0001,996,0002,269,184
3Sep 7, 2025 20:34659,778409,795,898381,672,000999,0002,269,184
4Sep 7, 2025 20:34671,472416,397,371389,454,00002,293,760
5Sep 7, 2025 20:34678,129417,473,113392,315,0001,000,0002,351,104
6Sep 7, 2025 20:34681,938428,425,762394,523,0001,001,0002,531,328
7Sep 7, 2025 20:34700,329427,528,719405,191,0001,000,0002,342,912
8Sep 7, 2025 20:34728,869451,668,453421,745,000999,0002,527,232
9Sep 7, 2025 20:34742,778460,802,724428,812,0001,999,0002,527,232
10Sep 7, 2025 20:34751,467467,146,613433,852,0001,999,0002,334,720