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 26, 2024 20:58E SequeiraE SequeiraScore: 78,181Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 26, 2024 20:5774,78164,905,53642,365,0001,008,0002,510,848
2Mar 26, 2024 20:5774,96264,336,85842,467,0001,011,0002,281,472
3Mar 26, 2024 20:5774,99066,773,90742,483,0001,011,0002,359,296
4Mar 26, 2024 20:5874,99568,131,19842,486,0001,011,0002,506,752
5Mar 26, 2024 20:5875,02265,624,11842,502,0001,011,0002,289,664
6Mar 26, 2024 20:5875,11466,801,18742,553,0001,013,0002,281,472
7Mar 26, 2024 20:5875,15367,724,98142,576,0001,013,0002,277,376
8Mar 26, 2024 20:5775,20273,809,84542,603,0001,014,0002,277,376
9Mar 26, 2024 20:5775,30769,749,58542,663,0001,015,0002,277,376
10Mar 26, 2024 20:5875,32669,919,08942,673,0001,016,0002,506,752
11Mar 26, 2024 20:5775,40070,619,95142,715,0001,017,0002,510,848
12Mar 26, 2024 20:5875,44872,142,00842,743,0001,017,0002,506,752
13Mar 26, 2024 20:5875,47871,804,21242,759,0001,018,0002,289,664
14Mar 26, 2024 20:5775,64572,078,95742,877,000997,0002,281,472
15Mar 26, 2024 20:5875,65070,669,75941,883,0001,994,0002,277,376
16Mar 26, 2024 20:5775,65067,843,80441,883,0001,994,0002,506,752
17Mar 26, 2024 20:5876,03873,781,70442,098,0002,004,0002,281,472
18Mar 26, 2024 20:5776,04767,740,83443,105,0001,002,0002,277,376
19Mar 26, 2024 20:5777,76070,345,12945,101,00002,285,568
20Mar 26, 2024 20:5777,90267,536,43343,175,0002,008,0002,351,104
21Mar 26, 2024 20:5878,00269,865,82843,231,0002,010,0002,285,568
22Mar 26, 2024 20:5878,18168,384,47744,338,0001,007,0002,265,088
23Mar 26, 2024 20:5878,18366,360,38443,331,0002,015,0002,502,656
24Mar 26, 2024 20:5878,26471,355,49344,385,0001,008,0002,510,848
25Mar 26, 2024 20:5878,29371,166,82943,392,0002,018,0002,285,568
26Mar 26, 2024 20:5878,30774,660,22245,418,00002,510,848
27Mar 26, 2024 20:5878,31071,900,99844,411,0001,009,0002,285,568
28Mar 26, 2024 20:5778,32871,504,30644,421,0001,009,0002,285,568
29Mar 26, 2024 20:5878,33164,967,24744,423,0001,009,0002,506,752
30Mar 26, 2024 20:5778,36283,078,50044,440,0001,010,0002,285,568
31Mar 26, 2024 20:5778,47870,434,11644,528,000989,0002,510,848
32Mar 26, 2024 20:5778,52973,790,64944,535,0001,012,0002,506,752
33Mar 26, 2024 20:5778,56671,976,69344,556,0001,012,0002,285,568
34Mar 26, 2024 20:5878,85372,476,78945,735,00002,506,752
35Mar 26, 2024 20:5779,12674,997,32444,896,000997,0002,510,848
36Mar 26, 2024 20:5779,40969,851,46544,055,0002,002,0002,285,568
37Mar 26, 2024 20:5782,470102,886,34447,832,64702,232,320
38Mar 26, 2024 20:5882,783109,272,68748,014,22002,252,800
39Mar 26, 2024 20:5883,247111,426,92848,283,36202,240,512
40Mar 26, 2024 20:5883,348112,602,02348,342,09502,228,224
41Mar 26, 2024 20:5783,362112,123,28248,349,81902,232,320
42Mar 26, 2024 20:5783,698104,475,26348,545,10702,224,128