Solving the system of equations for total cost