Add cost function.