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 16:31E SequeiraE SequeiraScore: 85,021Success
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 16:3184,25079,701,69248,865,00002,510,848
2Mar 26, 2024 16:3684,27475,259,50847,882,000997,0002,510,848
3Mar 26, 2024 16:3584,27878,273,00146,886,0001,995,0002,506,752
4Mar 26, 2024 16:3384,29373,435,54346,895,0001,995,0002,510,848
5Mar 26, 2024 16:3584,32178,280,02546,910,0001,996,0002,289,664
6Mar 26, 2024 16:3184,32975,513,43446,915,0001,996,0002,293,760
7Mar 26, 2024 16:3584,37173,003,39946,938,0001,997,0002,269,184
8Mar 26, 2024 16:3684,39073,851,11847,948,000998,0002,285,568
9Mar 26, 2024 16:3284,48470,576,41347,001,0002,000,0002,289,664
10Mar 26, 2024 16:3284,48672,608,99647,002,0002,000,0002,289,664
11Mar 26, 2024 16:3384,53670,476,77647,030,0002,001,0002,347,008
12Mar 26, 2024 16:3584,55272,421,44947,039,0002,001,0002,510,848
13Mar 26, 2024 16:3684,55379,237,87347,040,0002,001,0002,289,664
14Mar 26, 2024 16:3184,55372,684,61847,040,0002,001,0002,289,664
15Mar 26, 2024 16:3184,60576,765,75348,070,0001,001,0002,289,664
16Mar 26, 2024 16:3584,60971,136,68747,070,0002,003,0002,289,664
17Mar 26, 2024 16:3684,61070,794,09447,071,0002,003,0002,289,664
18Mar 26, 2024 16:3384,64370,545,71347,090,0002,003,0002,514,944
19Mar 26, 2024 16:3284,66771,581,02548,105,0001,002,0002,510,848
20Mar 26, 2024 16:3384,67676,008,40647,108,0002,004,0002,510,848
21Mar 26, 2024 16:3584,67871,894,64848,111,0001,002,0002,510,848
22Mar 26, 2024 16:3184,69071,750,78647,116,0002,004,0002,289,664
23Mar 26, 2024 16:3284,70271,473,21647,122,0002,005,0002,519,040
24Mar 26, 2024 16:3184,748106,466,55249,153,91002,224,128
25Mar 26, 2024 16:3584,75376,675,53647,151,0002,006,0002,289,664
26Mar 26, 2024 16:3684,805104,788,71549,187,05702,215,936
27Mar 26, 2024 16:3284,93371,691,53847,251,0002,010,0002,289,664
28Mar 26, 2024 16:3184,981111,338,67849,288,99502,215,936
29Mar 26, 2024 16:3184,98175,663,71347,278,0002,011,0002,277,376
30Mar 26, 2024 16:3384,99076,124,85748,288,0001,006,0002,285,568
31Mar 26, 2024 16:3585,02173,480,79047,300,0002,012,0002,371,584
32Mar 26, 2024 16:3385,034119,017,34149,319,92402,211,840
33Mar 26, 2024 16:3185,04572,386,71648,320,0001,006,0002,289,664
34Mar 26, 2024 16:3385,06972,499,81948,334,0001,006,0002,289,664
35Mar 26, 2024 16:3585,11476,399,25648,359,0001,007,0002,289,664
36Mar 26, 2024 16:3385,118111,082,27449,368,23802,207,744
37Mar 26, 2024 16:3285,11979,117,65948,362,0001,007,0002,289,664
38Mar 26, 2024 16:3385,12172,805,59648,363,0001,007,0002,506,752
39Mar 26, 2024 16:3385,14074,978,73747,366,0002,015,0002,289,664
40Mar 26, 2024 16:3285,15274,688,73747,373,0002,015,0002,510,848
41Mar 26, 2024 16:3585,163118,816,59249,394,82902,220,032
42Mar 26, 2024 16:3285,227117,254,98749,431,82302,211,840
43Mar 26, 2024 16:3285,26979,603,58848,447,0001,009,0002,273,280
44Mar 26, 2024 16:3685,27875,666,28548,452,0001,009,0002,502,656
45Mar 26, 2024 16:3685,28172,519,26348,454,0001,009,0002,289,664
46Mar 26, 2024 16:3385,32272,369,47648,478,0001,009,0002,289,664
47Mar 26, 2024 16:3685,33875,904,78749,496,00002,277,376
48Mar 26, 2024 16:3185,38673,568,34748,514,0001,010,0002,285,568
49Mar 26, 2024 16:3585,435115,400,77149,552,31702,220,032
50Mar 26, 2024 16:3185,46978,081,99648,561,0001,011,0002,289,664
51Mar 26, 2024 16:3285,48673,023,49148,571,0001,011,0002,351,104
52Mar 26, 2024 16:3685,69181,463,36648,687,0001,014,0002,519,040
53Mar 26, 2024 16:3585,697112,885,56949,704,44002,220,032
54Mar 26, 2024 16:3185,700114,318,67249,705,84902,224,128
55Mar 26, 2024 16:3685,833106,441,15449,783,15202,203,648
56Mar 26, 2024 16:3685,929117,397,19349,838,74202,211,840
57Mar 26, 2024 16:3285,948120,110,65149,849,64802,211,840
58Mar 26, 2024 16:3686,00285,748,48848,884,000997,0002,277,376
59Mar 26, 2024 16:3286,034109,151,51649,899,67402,220,032
60Mar 26, 2024 16:3386,062115,617,16849,915,83702,211,840