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 27, 2024 15:09E SequeiraE SequeiraScore: 77,403Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 27, 2024 15:1175,65271,961,78741,884,0001,994,0002,285,568
2Mar 27, 2024 15:1075,73367,031,82242,904,0001,021,0002,510,848
3Mar 27, 2024 15:1075,83668,155,36141,986,0001,999,0002,285,568
4Mar 27, 2024 15:1075,88168,282,35143,011,0001,000,0002,510,848
5Mar 27, 2024 15:0975,93474,154,89642,041,0002,001,0002,285,568
6Mar 27, 2024 15:1075,98466,761,84942,068,0002,003,0002,281,472
7Mar 27, 2024 15:1076,33873,889,18142,264,0002,012,0002,510,848
8Mar 27, 2024 15:1176,47268,855,58842,338,0002,016,0002,371,584
9Mar 27, 2024 15:1076,48666,733,25343,354,0001,008,0002,285,568
10Mar 27, 2024 15:0976,53171,456,64042,371,0002,017,0002,519,040
11Mar 27, 2024 15:1076,58873,166,22643,412,0001,009,0002,285,568
12Mar 27, 2024 15:1176,69167,463,35344,481,00002,510,848
13Mar 27, 2024 15:0976,71269,064,78743,482,0001,011,0002,510,848
14Mar 27, 2024 15:1176,81667,735,39843,541,0001,012,0002,519,040
15Mar 27, 2024 15:1077,01775,209,64344,670,00002,519,040
16Mar 27, 2024 15:1177,08170,480,19343,691,0001,016,0002,510,848
17Mar 27, 2024 15:0977,38471,789,44142,889,0001,994,0002,285,568
18Mar 27, 2024 15:1177,39770,323,07442,895,0001,995,0002,285,568
19Mar 27, 2024 15:0977,40373,794,55342,899,0001,995,0002,273,280
20Mar 27, 2024 15:1177,42872,131,30443,888,0001,020,0002,265,088
21Mar 27, 2024 15:0977,45267,633,88342,926,0001,996,0002,285,568
22Mar 27, 2024 15:1177,55968,676,73342,985,0001,999,0002,359,296
23Mar 27, 2024 15:1077,56467,456,58642,988,0001,999,0002,510,848
24Mar 27, 2024 15:0977,62671,047,77843,022,0002,001,0002,519,040
25Mar 27, 2024 15:0977,73668,596,85443,084,0002,003,0002,510,848
26Mar 27, 2024 15:0977,83871,938,18643,140,0002,006,0002,265,088
27Mar 27, 2024 15:1178,15272,016,36644,321,0001,007,0002,510,848
28Mar 27, 2024 15:1079,532105,902,52146,128,40802,228,224
29Mar 27, 2024 15:1079,625123,343,60946,182,37102,240,512
30Mar 27, 2024 15:1179,843106,742,67846,308,78502,232,320
31Mar 27, 2024 15:0980,084123,913,91346,448,53702,228,224
32Mar 27, 2024 15:1180,134112,808,57646,477,49802,232,320
33Mar 27, 2024 15:1180,321105,734,17946,586,46402,240,512
34Mar 27, 2024 15:0980,512121,633,77446,696,95302,240,512
35Mar 27, 2024 15:0981,107107,825,27047,042,28202,228,224
36Mar 27, 2024 15:1081,317109,461,81147,163,79702,248,704