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 20, 2025 23:05HattonuriHattonuriScore: 36,052Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 23:0432,62244,823,84116,930,0001,991,0002,527,232
2Dec 20, 2025 23:4832,70042,588,04816,970,0001,996,0002,293,760
3Dec 20, 2025 23:0532,89539,909,28618,075,0001,004,0002,396,160
4Dec 20, 2025 23:4833,83339,026,81318,591,0001,032,0002,293,760
5Dec 20, 2025 23:2734,31046,190,25217,910,0001,990,0002,527,232
6Dec 20, 2025 23:4834,41038,453,83617,963,0001,995,0002,527,232
7Dec 20, 2025 23:1734,46651,502,55818,991,000999,0002,523,136
8Dec 20, 2025 23:0534,56443,650,03218,043,0002,004,0002,527,232
9Dec 20, 2025 23:2734,64541,920,92119,090,0001,004,0002,265,088
10Dec 20, 2025 23:2734,73449,693,67819,139,0001,007,0002,527,232
11Dec 20, 2025 23:2735,15342,720,97819,370,0001,019,0002,289,664
12Dec 20, 2025 23:0435,37943,749,30519,494,0001,026,0002,519,040
13Dec 20, 2025 23:0535,59050,050,67819,610,0001,032,0002,527,232
14Dec 20, 2025 23:2836,05243,971,39018,919,0001,991,0002,281,472
15Dec 20, 2025 23:2836,05547,681,49819,917,000995,0002,519,040
16Dec 20, 2025 23:2736,06248,526,39918,924,0001,992,0002,519,040
17Dec 20, 2025 23:1736,21451,308,69419,004,0002,000,0002,519,040
18Dec 20, 2025 23:2836,22155,673,94619,008,0002,000,0002,293,760
19Dec 20, 2025 23:2736,29844,591,34119,048,0002,005,0002,285,568
20Dec 20, 2025 23:0536,30540,749,17019,052,0002,005,0002,523,136
21Dec 20, 2025 23:2736,34145,188,40820,075,0001,003,0002,519,040
22Dec 20, 2025 23:0436,36644,603,94619,084,0002,008,0002,519,040
23Dec 20, 2025 23:0536,40541,404,81119,104,0002,011,0002,519,040
24Dec 20, 2025 23:2736,42951,060,55719,117,0002,012,0002,523,136
25Dec 20, 2025 23:2736,84841,789,22720,355,0001,017,0002,289,664
26Dec 20, 2025 23:0537,14144,233,72820,517,0001,025,0002,289,664
27Dec 20, 2025 23:1737,76248,246,03520,907,000995,0002,519,040