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 04:56Josu San MartinJosu San MartinScore: 36,767Success
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 04:5134,68643,950,77119,113,0001,005,0002,281,472
2Dec 21, 2025 05:0134,98442,304,81219,277,0001,014,0002,445,312
3Dec 21, 2025 04:5135,04549,725,16220,326,00002,273,280
4Dec 21, 2025 04:5535,06246,843,32619,320,0001,016,0002,465,792
5Dec 21, 2025 05:0235,08842,409,87919,334,0001,017,0002,486,272
6Dec 21, 2025 04:5135,40742,287,11019,510,0001,026,0002,437,120
7Dec 21, 2025 05:0135,51047,326,76419,567,0001,029,0002,486,272
8Dec 21, 2025 05:0435,64750,562,46219,642,0001,033,0002,437,120
9Dec 21, 2025 05:0935,66650,202,22919,652,0001,034,0002,461,696
10Dec 21, 2025 05:0236,02150,957,25719,898,000994,0002,465,792
11Dec 21, 2025 05:0436,02950,806,78619,902,000995,0002,387,968
12Dec 21, 2025 05:0436,03347,425,47419,904,000995,0002,330,624
13Dec 21, 2025 05:0336,03452,014,78619,905,000995,0002,469,888
14Dec 21, 2025 05:0736,03846,422,21920,902,00002,387,968
15Dec 21, 2025 05:0236,04353,838,33019,910,000995,0002,449,408
16Dec 21, 2025 04:5136,04548,445,47318,915,0001,991,0002,326,528
17Dec 21, 2025 05:0436,05348,619,48118,920,0001,991,0002,449,408
18Dec 21, 2025 05:0936,09351,692,90718,941,0001,993,0002,437,120
19Dec 21, 2025 04:5536,18846,389,52619,990,000999,0002,469,888
20Dec 21, 2025 05:0336,20944,052,00919,001,0002,000,0002,400,256
21Dec 21, 2025 05:0236,22441,123,40419,009,0002,001,0002,355,200
22Dec 21, 2025 05:0436,24750,793,86219,021,0002,002,0002,416,640
23Dec 21, 2025 05:0436,33850,727,63420,073,0001,003,0002,428,928
24Dec 21, 2025 05:0936,35941,689,86520,084,0001,004,0002,445,312
25Dec 21, 2025 05:0436,37245,126,12919,087,0002,009,0002,396,160
26Dec 21, 2025 05:0336,45244,186,63820,136,0001,006,0002,359,296
27Dec 21, 2025 05:0136,47643,618,77120,149,0001,007,0002,453,504
28Dec 21, 2025 05:0236,51243,380,82519,161,0002,016,0002,334,720
29Dec 21, 2025 05:0236,54347,083,87519,177,0002,018,0002,473,984
30Dec 21, 2025 05:0436,54549,700,96120,187,0001,009,0002,375,680
31Dec 21, 2025 05:0636,69747,167,96818,244,0003,040,0002,318,336
32Dec 21, 2025 05:0236,76745,529,99520,310,0001,015,0002,523,136
33Dec 21, 2025 05:0436,83641,453,89020,348,0001,017,0002,371,584
34Dec 21, 2025 04:5136,83843,839,23820,349,0001,017,0002,461,696
35Dec 21, 2025 04:5136,85551,783,03420,359,0001,017,0002,506,752
36Dec 21, 2025 05:0136,86743,930,32421,383,00002,281,472
37Dec 21, 2025 05:0436,88348,849,62421,392,00002,285,568
38Dec 21, 2025 05:0336,88843,875,66820,377,0001,018,0002,445,312
39Dec 21, 2025 05:0336,90745,238,79021,406,00002,273,280
40Dec 21, 2025 05:0336,92151,896,25920,395,0001,019,0002,379,776
41Dec 21, 2025 05:0736,94746,100,83520,409,0001,020,0002,441,216
42Dec 21, 2025 04:5136,99348,230,88421,456,00002,478,080
43Dec 21, 2025 04:5137,00740,221,92020,442,0001,022,0002,437,120
44Dec 21, 2025 05:0237,01446,227,38521,468,00002,457,600
45Dec 21, 2025 05:0137,01945,478,15421,471,00002,387,968
46Dec 21, 2025 05:0237,05951,283,99921,494,00002,400,256
47Dec 21, 2025 05:0637,08342,820,81819,460,0002,048,0002,367,488
48Dec 21, 2025 04:5537,19140,110,97020,544,0001,027,0002,404,352
49Dec 21, 2025 05:0637,21743,617,99420,559,0001,027,0002,416,640
50Dec 21, 2025 04:5137,22945,642,18221,593,00002,379,776
51Dec 21, 2025 05:0237,25943,902,94220,581,0001,029,0002,478,080
52Dec 21, 2025 04:5637,28841,590,09221,627,00002,510,848
53Dec 21, 2025 04:5137,32948,843,81420,620,0001,031,0002,371,584
54Dec 21, 2025 05:0237,35949,223,91720,637,0001,031,0002,510,848
55Dec 21, 2025 05:0237,42651,286,97021,707,00002,404,352
56Dec 21, 2025 04:5137,42950,712,19920,676,0001,033,0002,347,008
57Dec 21, 2025 05:0137,48343,955,61320,705,0001,035,0002,506,752
58Dec 21, 2025 04:5637,62247,877,09520,830,000991,0002,510,848
59Dec 21, 2025 04:5137,75553,349,90920,903,000995,0002,404,352
60Dec 21, 2025 05:0737,77254,490,11319,917,0001,991,0002,441,216
61Dec 21, 2025 04:5637,78355,695,45020,918,000996,0002,490,368
62Dec 21, 2025 05:0437,82242,444,21620,940,000997,0002,273,280
63Dec 21, 2025 05:0438,32649,650,07220,209,0002,020,0002,437,120