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:45HattonuriHattonuriScore: 30,866Success
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:4529,08442,574,91715,877,000992,0002,523,136
2Dec 23, 2025 23:4629,15931,219,39514,923,0001,989,0002,519,040
3Dec 23, 2025 23:5829,43844,998,35216,070,0001,004,0002,326,528
4Dec 23, 2025 23:4929,46740,921,11616,086,0001,005,0002,527,232
5Dec 23, 2025 23:4429,52435,004,23616,117,0001,007,0002,330,624
6Dec 23, 2025 23:4629,70544,640,48216,216,0001,013,0002,322,432
7Dec 23, 2025 23:5829,72140,980,35917,238,00002,506,752
8Dec 23, 2025 23:4529,91435,020,03417,350,00002,490,368
9Dec 23, 2025 23:5929,92833,640,13216,337,0001,021,0002,506,752
10Dec 23, 2025 23:4830,04844,113,74317,428,00002,281,472
11Dec 23, 2025 23:4530,11733,162,36317,468,00002,498,560
12Dec 23, 2025 23:4430,13333,690,31916,449,0001,028,0002,498,560
13Dec 23, 2025 23:4430,34338,151,56416,564,0001,035,0002,506,752
14Dec 23, 2025 23:4730,37944,509,88316,584,0001,036,0002,523,136
15Dec 23, 2025 23:5830,47948,074,65516,639,0001,039,0002,478,080
16Dec 23, 2025 23:5830,51647,905,73816,716,000983,0002,527,232
17Dec 23, 2025 23:4530,56443,538,38317,727,00002,502,656
18Dec 23, 2025 23:4530,58343,793,26217,738,00002,506,752
19Dec 23, 2025 23:4430,86643,681,71116,908,000994,0002,473,984
20Dec 23, 2025 23:4830,86935,593,81716,910,000994,0002,506,752
21Dec 23, 2025 23:4530,87246,586,39015,917,0001,989,0002,473,984
22Dec 23, 2025 23:5830,87434,113,79816,913,000994,0002,473,984
23Dec 23, 2025 23:5930,87850,577,31417,909,00002,318,336
24Dec 23, 2025 23:4430,88130,021,05515,921,0001,990,0002,482,176
25Dec 23, 2025 23:4530,88637,306,77816,919,000995,0002,461,696
26Dec 23, 2025 23:4930,89043,389,47717,916,00002,293,760
27Dec 23, 2025 23:4730,92842,603,04916,942,000996,0002,482,176
28Dec 23, 2025 23:4530,96245,875,93915,963,0001,995,0002,310,144
29Dec 23, 2025 23:5831,02239,706,35816,994,000999,0002,510,848
30Dec 23, 2025 23:4931,17139,266,40817,075,0001,004,0002,490,368
31Dec 23, 2025 23:4831,21645,420,77616,094,0002,011,0002,494,464
32Dec 23, 2025 23:4531,26248,449,44116,118,0002,014,0002,506,752
33Dec 23, 2025 23:4431,26433,457,49817,126,0001,007,0002,437,120
34Dec 23, 2025 23:4731,27437,279,18017,132,0001,007,0002,506,752
35Dec 23, 2025 23:4631,27642,169,97817,133,0001,007,0002,281,472
36Dec 23, 2025 23:5931,82943,504,05617,436,0001,025,0002,486,272