Both the ways as discussed above
let price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] ; let price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] ; totalCost += Math . min ( price_first , price_second ) ; } }