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 22, 2025 00:31HattonuriHattonuriScore: 31,047Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 22, 2025 00:3029,84733,093,65916,293,0001,018,0002,342,912
2Dec 22, 2025 00:2829,90943,060,67317,347,00002,330,624
3Dec 22, 2025 00:2829,95542,892,78217,374,00002,326,528
4Dec 22, 2025 00:3030,06438,479,71316,412,0001,025,0002,506,752
5Dec 22, 2025 00:2930,12838,048,22017,474,00002,461,696
6Dec 22, 2025 00:2830,18345,020,80817,506,00002,519,040
7Dec 22, 2025 00:2830,19733,882,06417,514,00002,510,848
8Dec 22, 2025 00:2830,24833,458,73417,544,00002,523,136
9Dec 22, 2025 00:3030,35540,065,88317,606,00002,510,848
10Dec 22, 2025 00:3030,44841,922,95717,660,00002,510,848
11Dec 22, 2025 00:3030,47630,994,46217,676,00002,297,856
12Dec 22, 2025 00:2930,50235,521,30717,691,00002,510,848
13Dec 22, 2025 00:3030,54337,797,76217,715,00002,310,144
14Dec 22, 2025 00:3030,65341,124,82117,779,00002,338,816
15Dec 22, 2025 00:2830,86639,030,44617,902,00002,322,432
16Dec 22, 2025 00:3030,86745,076,26016,909,000994,0002,486,272
17Dec 22, 2025 00:3430,87437,567,93915,918,0001,989,0002,469,888
18Dec 22, 2025 00:3030,88143,483,10716,916,000995,0002,326,528
19Dec 22, 2025 00:2930,88143,555,96216,916,000995,0002,510,848
20Dec 22, 2025 00:3430,88145,120,35415,921,0001,990,0002,306,048
21Dec 22, 2025 00:3030,89134,285,70216,922,000995,0002,519,040
22Dec 22, 2025 00:3430,91741,600,28316,936,000996,0002,523,136
23Dec 22, 2025 00:3430,96938,924,08916,965,000997,0002,326,528
24Dec 22, 2025 00:2930,97642,659,08816,968,000998,0002,301,952
25Dec 22, 2025 00:3031,02841,415,38817,996,00002,523,136
26Dec 22, 2025 00:3431,04738,486,33017,007,0001,000,0002,306,048
27Dec 22, 2025 00:2831,09747,257,52217,034,0001,002,0002,490,368
28Dec 22, 2025 00:2831,10546,580,41717,039,0001,002,0002,289,664
29Dec 22, 2025 00:3031,11241,070,77017,043,0001,002,0002,506,752
30Dec 22, 2025 00:3031,22941,294,81817,107,0001,006,0002,457,600
31Dec 22, 2025 00:3031,27441,254,50517,132,0001,007,0002,293,760
32Dec 22, 2025 00:3031,30038,620,07617,146,0001,008,0002,486,272
33Dec 22, 2025 00:3031,31638,814,30516,145,0002,018,0002,400,256
34Dec 22, 2025 00:2931,37141,709,05416,174,0002,021,0002,400,256
35Dec 22, 2025 00:3031,37939,892,97717,189,0001,011,0002,330,624
36Dec 22, 2025 00:2831,41232,798,64316,195,0002,024,0002,535,424
37Dec 22, 2025 00:3131,52446,714,67618,284,00002,293,760
38Dec 22, 2025 00:2831,60551,325,62118,331,00002,281,472
39Dec 22, 2025 00:3431,61037,189,70917,316,0001,018,0002,490,368
40Dec 22, 2025 00:3031,62441,744,70317,323,0001,019,0002,306,048
41Dec 22, 2025 00:2931,70945,935,90017,370,0001,021,0002,506,752
42Dec 22, 2025 00:2831,74337,358,89518,411,00002,281,472
43Dec 22, 2025 00:3031,83135,512,04517,437,0001,025,0002,400,256
44Dec 22, 2025 00:3131,84537,153,59017,444,0001,026,0002,523,136
45Dec 22, 2025 00:2831,99343,511,05717,526,0001,030,0002,453,504
46Dec 22, 2025 00:2832,00742,609,00818,564,00002,428,928
47Dec 22, 2025 00:3032,01037,461,80718,566,00002,355,200
48Dec 22, 2025 00:3032,07639,222,41517,571,0001,033,0002,437,120
49Dec 22, 2025 00:2832,60745,223,92118,912,00002,486,272
50Dec 22, 2025 00:3132,71040,206,99617,974,000998,0002,482,176
51Dec 22, 2025 00:2832,89546,147,22017,071,0002,008,0002,453,504