Calculating the final cost