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 22:28HattonuriHattonuriScore: 37,516Success
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 22:3034,65543,404,50419,095,0001,005,0002,265,088
2Dec 20, 2025 22:2634,95251,528,43519,259,0001,013,0002,265,088
3Dec 20, 2025 22:2635,03640,468,23619,305,0001,016,0002,371,584
4Dec 20, 2025 22:2935,35946,258,73420,508,00002,265,088
5Dec 20, 2025 22:4235,41940,608,51719,516,0001,027,0002,285,568
6Dec 20, 2025 22:3035,47246,738,75719,546,0001,028,0002,281,472
7Dec 20, 2025 22:3035,52945,398,25419,577,0001,030,0002,527,232
8Dec 20, 2025 22:2936,15042,631,94918,971,0001,996,0002,527,232
9Dec 20, 2025 22:2636,20344,148,76318,999,0001,999,0002,527,232
10Dec 20, 2025 22:2436,38842,135,47920,100,0001,005,0002,269,184
11Dec 20, 2025 22:4336,54742,513,93520,188,0001,009,0002,527,232
12Dec 20, 2025 22:2436,58853,314,56820,211,0001,010,0002,281,472
13Dec 20, 2025 22:2937,12843,352,38121,534,00002,531,328
14Dec 20, 2025 22:2737,27942,110,53521,622,00002,363,392
15Dec 20, 2025 22:2437,32846,299,25621,650,00002,519,040
16Dec 20, 2025 22:2737,51640,157,21821,759,00002,523,136
17Dec 20, 2025 22:2837,74548,695,63020,897,000995,0002,519,040
18Dec 20, 2025 22:4337,76650,383,51020,909,000995,0002,535,424
19Dec 20, 2025 22:4337,99547,004,23620,034,0002,003,0002,519,040
20Dec 20, 2025 22:4238,00243,892,84921,040,0001,001,0002,535,424
21Dec 20, 2025 22:2638,09347,430,16222,094,00002,527,232
22Dec 20, 2025 22:2938,14544,239,25421,119,0001,005,0002,519,040
23Dec 20, 2025 22:4238,23146,119,43821,167,0001,007,0002,514,944
24Dec 20, 2025 22:2638,26943,188,18621,188,0001,008,0002,519,040
25Dec 20, 2025 22:2938,29043,712,19421,199,0001,009,0002,531,328
26Dec 20, 2025 22:2938,32644,309,97921,219,0001,010,0002,519,040
27Dec 20, 2025 22:2838,33844,348,87821,226,0001,010,0002,527,232
28Dec 20, 2025 22:2738,35538,008,95919,213,0003,033,0002,519,040
29Dec 20, 2025 22:2638,38642,821,10620,240,0002,024,0002,363,392
30Dec 20, 2025 22:2838,48346,251,95320,291,0002,029,0002,519,040