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 23, 2025 23:34HattonuriHattonuriScore: 30,590Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 23, 2025 23:3929,52640,788,13116,118,0001,007,0002,314,240
2Dec 23, 2025 23:4029,55237,621,85616,132,0001,008,0002,510,848
3Dec 23, 2025 23:4029,72834,774,55417,242,00002,338,816
4Dec 23, 2025 23:3929,90736,912,68017,346,00002,310,144
5Dec 23, 2025 23:4029,94545,029,21716,347,0001,021,0002,510,848
6Dec 23, 2025 23:4030,04338,941,56817,425,00002,482,176
7Dec 23, 2025 23:3730,26433,661,49717,553,00002,334,720
8Dec 23, 2025 23:3430,32144,109,87217,586,00002,506,752
9Dec 23, 2025 23:3730,40030,928,30317,632,00002,306,048
10Dec 23, 2025 23:3830,45940,572,86917,666,00002,301,952
11Dec 23, 2025 23:3830,50039,565,80817,690,00002,502,656
12Dec 23, 2025 23:3530,50243,841,14417,691,00002,486,272
13Dec 23, 2025 23:3930,54535,184,05416,674,0001,042,0002,482,176
14Dec 23, 2025 23:3530,59036,339,63915,771,0001,971,0002,510,848
15Dec 23, 2025 23:3830,59744,893,98117,746,00002,289,664
16Dec 23, 2025 23:4030,86941,887,27116,910,000994,0002,433,024
17Dec 23, 2025 23:4030,87442,525,90116,913,000994,0002,519,040
18Dec 23, 2025 23:3930,90942,028,70417,927,00002,510,848
19Dec 23, 2025 23:3530,96440,763,51715,964,0001,995,0002,494,464
20Dec 23, 2025 23:3931,02146,602,60516,993,000999,0002,310,144
21Dec 23, 2025 23:3531,03140,970,81116,999,000999,0002,482,176
22Dec 23, 2025 23:3931,13645,905,68916,053,0002,006,0002,486,272
23Dec 23, 2025 23:3531,22945,838,35317,107,0001,006,0002,527,232
24Dec 23, 2025 23:3431,35338,636,41317,175,0001,010,0002,387,968
25Dec 23, 2025 23:3731,43135,838,64618,230,00002,527,232
26Dec 23, 2025 23:3531,53139,676,24218,288,00002,523,136
27Dec 23, 2025 23:3431,91046,691,45918,508,00002,478,080