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:34HattonuriHattonuriError
Source Code

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

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