calculating cost of optimized path done