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 21, 2025 02:25HattonuriHattonuriScore: 32,612Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 21, 2025 02:2130,50347,595,67717,692,00002,531,328
2Dec 21, 2025 02:2131,07239,926,70716,020,0002,002,0002,506,752
3Dec 21, 2025 02:2531,12146,964,97418,050,00002,510,848
4Dec 21, 2025 02:2131,13443,478,85417,055,0001,003,0002,502,656
5Dec 21, 2025 02:2231,20741,952,86917,095,0001,005,0002,523,136
6Dec 21, 2025 02:2531,25341,175,62516,113,0002,014,0002,326,528
7Dec 21, 2025 02:2531,33148,663,81916,153,0002,019,0002,306,048
8Dec 21, 2025 02:2131,62637,585,40318,343,00002,318,336
9Dec 21, 2025 02:2531,69540,486,55018,383,00002,519,040
10Dec 21, 2025 02:2131,69835,448,55618,385,00002,334,720
11Dec 21, 2025 02:2531,72838,119,86017,380,0001,022,0002,494,464
12Dec 21, 2025 02:2231,72941,216,48418,403,00002,494,464
13Dec 21, 2025 02:2131,75038,935,08518,415,00002,318,336
14Dec 21, 2025 02:2131,79140,177,32817,415,0001,024,0002,326,528
15Dec 21, 2025 02:2131,81739,877,95017,429,0001,025,0002,519,040
16Dec 21, 2025 02:2531,84741,703,86817,445,0001,026,0002,326,528
17Dec 21, 2025 02:2332,10943,209,54318,623,00002,334,720
18Dec 21, 2025 02:2132,11238,847,84617,591,0001,034,0002,408,448
19Dec 21, 2025 02:2232,18640,115,33318,668,00002,506,752
20Dec 21, 2025 02:2532,22846,510,01617,654,0001,038,0002,318,336
21Dec 21, 2025 02:2332,25248,628,99617,667,0001,039,0002,326,528
22Dec 21, 2025 02:2132,26643,012,88218,714,00002,297,856
23Dec 21, 2025 02:2532,59543,258,15317,910,000995,0002,297,856
24Dec 21, 2025 02:2132,59843,311,57017,912,000995,0002,293,760
25Dec 21, 2025 02:2132,61038,988,39518,914,00002,326,528
26Dec 21, 2025 02:2132,61244,906,96317,920,000995,0002,478,080
27Dec 21, 2025 02:2332,62445,432,46017,927,000995,0002,494,464
28Dec 21, 2025 02:2432,63346,190,31717,931,000996,0002,506,752
29Dec 21, 2025 02:2232,95937,460,00318,110,0001,006,0002,510,848
30Dec 21, 2025 02:2133,03840,297,99618,154,0001,008,0002,338,816
31Dec 21, 2025 02:2133,09344,655,24717,174,0002,020,0002,285,568
32Dec 21, 2025 02:2433,14134,388,87518,211,0001,011,0002,310,144
33Dec 21, 2025 02:2433,20742,524,25017,233,0002,027,0002,437,120
34Dec 21, 2025 02:2333,21441,996,46818,251,0001,013,0002,498,560
35Dec 21, 2025 02:2533,24044,127,07918,265,0001,014,0002,502,656
36Dec 21, 2025 02:2533,60035,796,85118,463,0001,025,0002,297,856
37Dec 21, 2025 02:2133,69135,863,47018,513,0001,028,0002,478,080
38Dec 21, 2025 02:2133,73838,004,03718,539,0001,029,0002,465,792
39Dec 21, 2025 02:2333,88442,622,33519,653,00002,449,408
40Dec 21, 2025 02:2234,08147,597,48218,727,0001,040,0002,502,656
41Dec 21, 2025 02:2134,28844,059,62917,899,0001,988,0002,510,848
42Dec 21, 2025 02:2534,30745,921,63017,909,0001,989,0002,510,848
43Dec 21, 2025 02:2334,31037,124,91517,910,0001,990,0002,478,080
44Dec 21, 2025 02:2234,31648,465,08417,913,0001,990,0002,535,424
45Dec 21, 2025 02:2134,31645,521,16417,913,0001,990,0002,301,952
46Dec 21, 2025 02:2134,31647,776,20817,913,0001,990,0002,519,040
47Dec 21, 2025 02:2534,31750,487,93418,909,000995,0002,457,600
48Dec 21, 2025 02:2534,31949,481,81817,915,0001,990,0002,461,696
49Dec 21, 2025 02:2134,32948,213,65418,916,000995,0002,441,216
50Dec 21, 2025 02:2534,56043,069,41218,041,0002,004,0002,510,848
51Dec 21, 2025 02:2534,60938,126,82618,066,0002,007,0002,371,584