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 26, 2025 11:11HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 20:42Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 20:4202,765,679,1232,757,672,000016,384No answer to stdout. Died or killed?
2Dec 26, 2025 11:0728,07140,425,69216,281,00002,347,008
3Dec 26, 2025 11:1028,28343,546,08916,404,00002,342,912
4Dec 26, 2025 11:0928,86245,522,89516,740,00002,510,848
5Dec 26, 2025 11:0529,14547,146,44715,910,000994,0002,527,232
6Dec 26, 2025 11:0929,14534,984,00214,916,0001,988,0002,519,040
7Dec 26, 2025 11:0729,14744,506,07616,905,00002,301,952
8Dec 26, 2025 11:0729,14843,225,02315,912,000994,0002,334,720
9Dec 26, 2025 11:0929,16942,153,22914,928,0001,990,0002,334,720
10Dec 26, 2025 11:0729,17149,200,74715,924,000995,0002,326,528
11Dec 26, 2025 11:1029,19845,136,64215,939,000996,0002,310,144
12Dec 26, 2025 11:1129,28438,649,27215,986,000999,0002,281,472
13Dec 26, 2025 11:1129,30733,278,69815,999,000999,0002,498,560
14Dec 26, 2025 11:0729,36937,157,72116,032,0001,002,0002,342,912
15Dec 26, 2025 11:0929,39139,035,44615,042,0002,005,0002,506,752
16Dec 26, 2025 11:1029,41738,498,24615,055,0002,007,0002,494,464
17Dec 26, 2025 11:0529,55341,779,87916,133,0001,008,0002,322,432
18Dec 26, 2025 11:0529,77236,830,99617,268,00002,281,472
19Dec 26, 2025 11:0729,79340,700,71917,280,00002,523,136
20Dec 26, 2025 11:0529,81247,169,02117,291,00002,334,720
21Dec 26, 2025 11:0529,81241,863,83617,291,00002,502,656
22Dec 26, 2025 11:0729,85544,490,17617,316,00002,465,792
23Dec 26, 2025 11:0529,86630,524,58617,322,00002,306,048
24Dec 26, 2025 11:1029,87440,320,25517,327,00002,310,144
25Dec 26, 2025 11:1029,87439,362,10716,308,0001,019,0002,416,640
26Dec 26, 2025 11:0929,97242,590,44817,384,00002,281,472
27Dec 26, 2025 11:1030,00031,571,06717,400,00002,285,568
28Dec 26, 2025 11:0730,02244,286,67217,413,00002,330,624
29Dec 26, 2025 11:0530,02241,706,71317,413,00002,465,792
30Dec 26, 2025 11:0730,03838,586,91517,422,00002,461,696
31Dec 26, 2025 11:0530,05939,420,69917,434,00002,465,792
32Dec 26, 2025 11:1130,11735,996,91216,441,0001,027,0002,465,792
33Dec 26, 2025 11:0730,14347,554,93517,483,00002,461,696
34Dec 26, 2025 11:1130,16038,597,73717,493,00002,519,040
35Dec 26, 2025 11:0730,25038,201,71317,545,00002,428,928
36Dec 26, 2025 11:1130,33145,452,96117,592,00002,461,696
37Dec 26, 2025 11:0730,36740,080,98917,613,00002,498,560
38Dec 26, 2025 11:0730,37635,033,62717,618,00002,461,696
39Dec 26, 2025 11:1130,49146,140,20517,685,00002,494,464
40Dec 26, 2025 11:0530,52244,629,66717,703,00002,408,448
41Dec 26, 2025 11:0730,62843,213,09017,764,00002,420,736
42Dec 26, 2025 11:0730,87430,747,95116,913,000994,0002,428,928
43Dec 26, 2025 11:0930,95738,265,99615,960,0001,995,0002,310,144