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 listApr 2, 2025 04:27Yuriy LyfenkoYuriy LyfenkoScore: 45,571Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 2, 2025 13:5441,49549,529,26222,062,0002,005,0002,363,392
2Mar 18, 2026 19:3242,12846,470,90824,434,00002,351,104
3Apr 2, 2025 04:2342,19043,494,33123,451,0001,019,0002,437,120
4Apr 2, 2025 04:2442,47448,836,66523,609,0001,026,0002,269,184
5Apr 2, 2025 13:5542,47648,575,50024,636,00002,363,392
6Apr 2, 2025 13:5542,55049,144,40024,679,00002,359,296
7Apr 2, 2025 14:5442,65552,255,58924,740,00002,351,104
8Apr 2, 2025 13:5542,93151,084,52122,908,0001,992,0002,351,104
9Apr 2, 2025 13:5542,93453,036,73223,906,000996,0002,342,912
10Apr 2, 2025 14:5442,94748,125,81022,917,0001,992,0002,351,104
11Apr 2, 2025 04:2443,05047,380,54522,972,0001,997,0002,355,200
12Apr 2, 2025 14:5543,16259,720,31624,033,0001,001,0002,338,816
13Apr 2, 2025 13:5443,17448,344,63923,038,0002,003,0002,510,848
14Apr 2, 2025 04:2443,18148,889,18624,044,0001,001,0002,342,912
15Apr 2, 2025 14:5443,22149,141,90723,063,0002,005,0002,314,240
16Apr 2, 2025 04:2443,25046,695,00523,079,0002,006,0002,502,656
17Apr 2, 2025 14:5443,45946,699,78423,190,0002,016,0002,502,656
18Apr 2, 2025 13:5543,59046,877,76225,282,00002,318,336
19Apr 2, 2025 13:5443,63349,532,17723,283,0002,024,0002,322,432
20Apr 2, 2025 13:5543,81747,880,34325,414,00002,502,656
21Apr 2, 2025 14:5543,82848,086,48725,420,00002,347,008
22Apr 2, 2025 13:5543,83352,981,82624,407,0001,016,0002,310,144
23Apr 2, 2025 14:5443,86052,342,75824,422,0001,017,0002,502,656
24Apr 2, 2025 14:5543,99045,799,01124,494,0001,020,0002,498,560
25Apr 2, 2025 13:5444,24353,767,73824,635,0001,026,0002,510,848
26Apr 2, 2025 13:5544,30750,825,85224,671,0001,027,0002,326,528
27Apr 2, 2025 04:2344,34551,808,19424,692,0001,028,0002,490,368
28Apr 2, 2025 13:5544,39056,570,15025,746,00002,510,848
29Apr 2, 2025 13:5544,65749,571,73823,909,0001,992,0002,310,144
30Apr 2, 2025 04:2344,66251,587,54023,912,0001,992,0002,478,080
31Apr 2, 2025 14:5544,66450,886,82524,909,000996,0002,297,856
32Apr 2, 2025 04:2344,77948,313,68524,974,000998,0002,506,752
33Apr 2, 2025 04:2444,85248,162,75224,013,0002,001,0002,506,752
34Apr 2, 2025 14:5544,86252,798,20424,019,0002,001,0002,289,664
35Apr 2, 2025 14:5544,90753,767,38924,043,0002,003,0002,490,368
36Apr 2, 2025 04:2345,12457,739,31024,159,0002,013,0002,306,048
37Apr 2, 2025 14:5545,14151,279,33225,175,0001,007,0002,510,848
38Apr 2, 2025 04:2445,19149,231,23524,195,0002,016,0002,478,080
39Apr 2, 2025 14:5545,40246,518,47725,321,0001,012,0002,371,584
40Apr 2, 2025 14:5445,57152,502,73425,415,0001,016,0002,318,336
41Apr 2, 2025 04:2445,64850,359,92526,476,00002,457,600
42Apr 2, 2025 04:2445,72152,944,19026,518,00002,293,760
43Apr 2, 2025 14:5445,91656,126,73425,607,0001,024,0002,428,928
44Apr 2, 2025 13:5446,02857,890,41725,670,0001,026,0002,465,792
45Apr 2, 2025 14:5446,39148,051,56724,914,0001,993,0002,449,408
46Apr 2, 2025 14:5546,53149,102,81024,989,0001,999,0002,441,216
47Apr 2, 2025 14:5446,65051,760,93426,055,0001,002,0002,531,328
48Apr 2, 2025 04:2346,66448,888,02326,063,0001,002,0002,453,504
49Apr 2, 2025 14:5546,75352,508,26025,109,0002,008,0002,486,272
50Apr 2, 2025 04:2346,95553,366,86125,217,0002,017,0002,289,664
51Apr 2, 2025 04:2347,06755,101,49226,288,0001,011,0002,293,760
52Apr 2, 2025 04:2447,17248,231,89826,347,0001,013,0002,314,240
53Apr 2, 2025 13:5547,19053,322,46926,357,0001,013,0002,469,888
54Apr 2, 2025 04:2447,30051,719,73826,418,0001,016,0002,457,600
55Apr 2, 2025 13:5547,82655,171,08726,712,0001,027,0002,297,856
56Apr 2, 2025 13:5548,09358,790,70226,898,000996,0002,392,064
57Apr 2, 2025 04:2448,20053,268,02524,961,0002,995,0002,306,048
58Apr 2, 2025 13:5548,42249,237,04726,079,0002,006,0002,486,272
59Apr 2, 2025 13:5548,81056,624,33527,299,0001,011,0002,437,120
60Apr 2, 2025 14:5549,25948,065,72627,550,0001,020,0002,531,328
61Apr 2, 2025 04:2449,43654,514,43428,673,00002,297,856
62Apr 2, 2025 04:2449,44056,702,57427,651,0001,024,0002,379,776
63Mar 18, 2026 19:3249,49351,247,98327,681,0001,025,0002,519,040
64Apr 2, 2025 13:5550,09750,771,86227,053,0002,003,0002,535,424
65Apr 2, 2025 13:5450,11052,620,31428,062,0001,002,0002,494,464
66Apr 2, 2025 13:5550,15351,184,74127,083,0002,006,0002,527,232
67Apr 2, 2025 04:2450,28157,808,40628,158,0001,005,0002,469,888
68Apr 2, 2025 04:2450,62250,925,60328,349,0001,012,0002,437,120
69Mar 18, 2026 19:3250,71254,119,34628,399,0001,014,0002,428,928
70Apr 2, 2025 04:2350,71955,648,85328,403,0001,014,0002,510,848
71Apr 2, 2025 14:5551,25759,612,84729,729,00002,506,752
72Apr 2, 2025 13:5451,81252,574,42028,048,0002,003,0002,355,200
73Apr 2, 2025 13:5454,46452,196,51731,589,00002,498,560
74Apr 2, 2025 13:5454,59757,239,31530,645,0001,021,0002,420,736
75Apr 2, 2025 13:5555,03358,298,58131,919,00002,453,504
76Apr 2, 2025 04:2794,70580,438,01952,932,0001,997,0002,506,752
77Apr 2, 2025 04:2795,62678,209,89955,463,00002,473,984
78Apr 2, 2025 04:2796,57284,631,12354,012,0002,000,0002,473,984