Find the minimum cost to cross the River | Function to return the minimum cost ; Sort the price array ; Calculate minimum price of n - 2 most costly person ; Both the ways as discussed above ; Calculate the minimum price of the two cheapest person ; Driver code
def minimumCost ( price , n ) : NEW_LINE INDENT price = sorted ( price ) NEW_LINE totalCost = 0 NEW_LINE for i in range ( n - 1 , 1 , - 2 ) : NEW_LINE INDENT if ( i == 2 ) : NEW_LINE INDENT totalCost += price [ 2 ] + price [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] NEW_LINE price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] NEW_LINE totalCost += min ( price_first , price_second ) NEW_LINE DEDENT DEDENT if ( n == 1 ) : NEW_LINE INDENT totalCost += price [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT totalCost += price [ 1 ] NEW_LINE DEDENT return totalCost NEW_LINE DEDENT price = [ 30 , 40 , 60 , 70 ] NEW_LINE n = len ( price ) NEW_LINE print ( minimumCost ( price , n ) ) NEW_LINE