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 listFeb 7, 2025 11:41Daniel HarrisDaniel HarrisError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:59Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 19:00011,484,89301,430,00028,672Exit with code 101: thread 'main' panicked at main.rs:68:9: index out of bounds: the len is 8192 but the index is 21692 note: run with `RUST_BACKTRACE=1` environment variable to display a backtracestderr
2Feb 7, 2025 11:40351,922230,106,236203,115,0001,000,0002,465,792
3Feb 7, 2025 11:40356,200238,009,564205,594,0001,002,0002,453,504
4Feb 7, 2025 11:40375,483250,110,158215,783,0001,997,0002,441,216
5Feb 7, 2025 11:40382,469250,996,818219,834,0001,998,0002,457,600
6Feb 7, 2025 11:41392,433254,652,744227,611,00002,461,696
7Feb 7, 2025 11:41394,769254,264,917226,967,0001,999,0002,445,312
8Feb 7, 2025 11:40400,071257,170,155230,041,0002,000,0002,433,024
9Feb 7, 2025 11:41421,714270,749,099243,592,0001,002,0002,453,504
10Feb 7, 2025 11:41441,952279,970,930256,332,00002,445,312
11Feb 7, 2025 11:41458,200290,428,775263,758,0001,998,0002,437,120
12Feb 7, 2025 11:41465,672290,639,131267,089,0003,001,0002,461,696
13Feb 7, 2025 11:40467,472299,094,000270,134,0001,000,0002,428,928
14Feb 7, 2025 11:41477,174307,695,851276,761,00002,457,600
15Feb 7, 2025 11:41487,831312,661,321281,943,000999,0002,437,120
16Feb 7, 2025 11:41500,098312,312,550288,057,0002,000,0002,412,544
17Feb 7, 2025 11:40503,091323,004,893290,794,000999,0002,510,848
18Feb 7, 2025 11:41544,410342,269,958313,760,0001,998,0002,420,736
19Feb 7, 2025 11:40565,038355,560,822327,722,00002,400,256
20Feb 7, 2025 11:41573,222368,650,444331,468,0001,001,0002,441,216
21Feb 7, 2025 11:40579,902369,781,643335,342,0001,001,0002,392,064
22Feb 7, 2025 11:41587,345369,096,723338,662,0001,998,0002,428,928