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 listMar 31, 2025 17:42Yuriy LyfenkoYuriy LyfenkoScore: 62,597Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 31, 2025 18:0256,68863,424,60330,887,0001,992,0002,289,664
2Mar 31, 2025 18:0356,75756,033,36030,924,0001,995,0002,510,848
3Mar 31, 2025 18:0357,09766,063,11631,109,0002,007,0002,502,656
4Mar 31, 2025 18:0257,34060,212,33431,242,0002,015,0002,523,136
5Mar 31, 2025 18:0257,46057,036,08331,308,0002,019,0002,437,120
6Mar 31, 2025 18:0357,54757,418,34632,366,0001,011,0002,412,544
7Mar 31, 2025 18:0357,79757,336,21832,507,0001,015,0002,424,832
8Mar 31, 2025 18:0257,84854,827,05632,536,0001,016,0002,265,088
9Mar 31, 2025 18:0157,85555,641,66232,540,0001,016,0002,523,136
10Mar 31, 2025 18:0358,40962,843,22431,885,0001,992,0002,412,544
11Mar 31, 2025 18:0258,73457,409,75531,061,0003,005,0002,523,136
12Mar 31, 2025 18:0258,78659,081,35132,091,0002,005,0002,404,352
13Mar 31, 2025 18:0359,22657,256,85032,331,0002,020,0002,506,752
14Mar 31, 2025 18:0259,23670,841,26232,336,0002,021,0002,502,656
15Mar 31, 2025 18:0359,32859,011,50633,398,0001,012,0002,396,160
16Mar 31, 2025 18:0259,48655,466,98532,473,0002,029,0002,510,848
17Mar 31, 2025 18:0360,11660,429,32032,875,0001,992,0002,469,888
18Mar 31, 2025 18:0260,15566,105,23332,897,0001,993,0002,441,216
19Mar 31, 2025 18:0160,37658,624,44933,017,0002,001,0002,416,640
20Mar 31, 2025 18:0360,77460,858,45133,235,0002,014,0002,486,272
21Mar 31, 2025 18:0361,02864,012,95134,385,0001,011,0002,523,136
22Mar 31, 2025 18:0361,11959,722,98634,437,0001,012,0002,473,984
23Mar 31, 2025 18:0261,88362,105,17634,895,000997,0002,363,392
24Mar 31, 2025 18:0362,29862,936,13434,126,0002,007,0002,375,680
25Mar 31, 2025 18:0362,30762,480,24634,131,0002,007,0002,408,448
26Mar 31, 2025 18:0162,59759,372,73534,289,0002,017,0002,416,640
27Mar 31, 2025 18:0163,19869,273,66535,637,0001,018,0002,338,816
28Mar 31, 2025 18:0263,58167,236,84035,881,000996,0002,469,888
29Mar 31, 2025 18:0163,80961,125,48535,009,0002,000,0002,490,368
30Mar 31, 2025 18:0263,87258,447,12335,044,0002,002,0002,265,088
31Mar 31, 2025 18:0264,61059,360,26436,462,0001,012,0002,273,280
32Mar 31, 2025 18:0264,68660,498,01937,518,00002,420,736
33Mar 31, 2025 18:0264,91662,693,38836,634,0001,017,0002,396,160
34Mar 31, 2025 18:0365,76262,155,71336,135,0002,007,0002,441,216
35Mar 31, 2025 18:0365,79763,417,14536,154,0002,008,0002,404,352
36Mar 31, 2025 18:0366,22166,955,80037,398,0001,010,0002,371,584
37Mar 31, 2025 18:0267,47863,347,55337,130,0002,007,0002,461,696
38Mar 31, 2025 18:0267,83360,583,82037,326,0002,017,0002,473,984
39Mar 31, 2025 18:0368,62162,885,31736,815,0002,985,0002,445,312
40Mar 31, 2025 18:0371,28165,359,18740,335,0001,008,0002,306,048
41Mar 31, 2025 18:0272,38170,682,68839,982,0001,999,0002,428,928
42Mar 31, 2025 18:0174,79867,974,52642,375,0001,008,0002,428,928
43Mar 31, 2025 17:42105,96683,630,45360,453,0001,007,0002,482,176
44Mar 31, 2025 17:42106,72484,654,69860,886,0001,014,0002,502,656
45Mar 31, 2025 17:42106,82888,341,90359,962,0001,998,0002,498,560
46Mar 31, 2025 17:42106,84884,553,48159,973,0001,999,0002,506,752
47Mar 31, 2025 17:42106,93385,749,10460,021,0002,000,0002,502,656
48Mar 31, 2025 17:42110,35785,647,67562,007,0002,000,0002,494,464
49Mar 31, 2025 17:42111,53893,749,13663,682,0001,010,0002,510,848
50Mar 31, 2025 17:42112,12889,336,60463,033,0002,001,0002,387,968
51Mar 31, 2025 17:42117,74794,620,00167,289,0001,004,0002,375,680