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 24, 2025 03:00RussellStrongRussellStrongScore: 110,126Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 24, 2025 02:59108,71788,693,72362,056,0001,000,0002,363,392
2Dec 24, 2025 03:00108,86084,765,55659,131,0004,008,0002,289,664
3Dec 24, 2025 03:00109,01486,178,31860,218,0003,010,0002,289,664
4Dec 24, 2025 03:00109,07185,938,97762,257,0001,004,0002,289,664
5Dec 24, 2025 03:00109,20286,484,00962,332,0001,005,0002,355,200
6Dec 24, 2025 03:00109,35785,535,17262,421,0001,006,0002,363,392
7Dec 24, 2025 02:59109,58887,319,60960,535,0003,026,0002,342,912
8Dec 24, 2025 03:00109,62890,870,88863,584,00002,351,104
9Dec 24, 2025 02:59109,82290,169,92461,675,0002,022,0002,359,296
10Dec 24, 2025 03:00109,87288,643,36463,726,00002,289,664
11Dec 24, 2025 02:59109,87688,910,00562,717,0001,011,0002,355,200
12Dec 24, 2025 02:59110,06491,192,31462,840,000997,0002,359,296
13Dec 24, 2025 03:00110,12685,075,14261,877,0001,996,0002,469,888
14Dec 24, 2025 03:00110,14389,142,08059,891,0003,992,0002,355,200
15Dec 24, 2025 02:59110,14894,390,82560,892,0002,994,0002,289,664
16Dec 24, 2025 02:59110,16695,507,41260,901,0002,995,0002,289,664
17Dec 24, 2025 03:00110,24788,972,04660,946,0002,997,0002,289,664
18Dec 24, 2025 03:00110,29792,632,03159,974,0003,998,0002,359,296
19Dec 24, 2025 03:00110,34799,458,50261,001,0003,000,0002,359,296
20Dec 24, 2025 03:00110,44189,702,40463,056,0001,000,0002,351,104
21Dec 24, 2025 03:00110,57891,638,68261,129,0003,006,0002,351,104
22Dec 24, 2025 03:00110,82189,968,65963,272,0001,004,0002,359,296
23Dec 24, 2025 02:59110,96689,413,19563,355,0001,005,0002,351,104
24Dec 24, 2025 02:59110,98690,036,38062,361,0002,011,0002,289,664