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:02HattonuriHattonuriScore: 29,991Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 11:1327,97633,967,34415,212,0001,014,0002,347,008
2Dec 26, 2025 11:1228,18133,442,01216,345,00002,342,912
3Dec 26, 2025 12:0728,20532,938,23716,359,00002,351,104
4Dec 26, 2025 12:0728,47241,294,88716,514,00002,338,816
5Dec 26, 2025 11:3729,15043,972,41116,907,00002,510,848
6Dec 26, 2025 11:0229,15544,531,25214,921,0001,989,0002,289,664
7Dec 26, 2025 11:1329,16047,843,40715,919,000994,0002,342,912
8Dec 26, 2025 11:2129,16445,707,20715,920,000995,0002,334,720
9Dec 26, 2025 11:1329,39036,369,58816,044,0001,002,0002,506,752
10Dec 26, 2025 11:1229,41432,675,80116,057,0001,003,0002,494,464
11Dec 26, 2025 11:1329,44038,323,14816,071,0001,004,0002,498,560
12Dec 26, 2025 11:3729,45934,000,99716,081,0001,005,0002,486,272
13Dec 26, 2025 11:1229,55941,481,16215,127,0002,017,0002,318,336
14Dec 26, 2025 11:0229,59838,251,81716,158,0001,009,0002,306,048
15Dec 26, 2025 11:0229,82442,337,35415,263,0002,035,0002,306,048
16Dec 26, 2025 11:0229,82840,218,62116,283,0001,017,0002,494,464
17Dec 26, 2025 11:2129,86942,155,80117,324,00002,523,136
18Dec 26, 2025 11:1329,91940,524,72316,333,0001,020,0002,473,984
19Dec 26, 2025 11:0229,99148,151,17217,395,00002,510,848
20Dec 26, 2025 11:0229,99530,087,95017,397,00002,498,560
21Dec 26, 2025 11:2130,02449,659,63017,414,00002,486,272
22Dec 26, 2025 12:0730,04334,331,83417,425,00002,326,528
23Dec 26, 2025 11:0230,05238,407,25516,405,0001,025,0002,281,472
24Dec 26, 2025 12:0730,11435,981,63217,466,00002,285,568
25Dec 26, 2025 11:3730,13348,863,15817,477,00002,289,664
26Dec 26, 2025 11:1330,14338,218,11316,455,0001,028,0002,281,472
27Dec 26, 2025 11:1330,22138,250,64116,497,0001,031,0002,449,408
28Dec 26, 2025 12:0730,22246,339,86816,498,0001,031,0002,281,472
29Dec 26, 2025 12:0730,22836,777,37517,532,00002,461,696
30Dec 26, 2025 12:0730,23436,260,48416,505,0001,031,0002,457,600
31Dec 26, 2025 11:1330,25543,089,22416,516,0001,032,0002,486,272
32Dec 26, 2025 11:0230,30737,059,63416,544,0001,034,0002,531,328
33Dec 26, 2025 12:0730,44144,491,83517,656,00002,420,736
34Dec 26, 2025 11:1330,49542,133,70817,687,00002,506,752
35Dec 26, 2025 12:0730,57844,180,12616,692,0001,043,0002,424,832
36Dec 26, 2025 11:0230,94843,909,33617,950,00002,383,872